Nowadays everybody is working on transforming his body in gyms so our ninja is also interested in transformation. But as our ninja is interested in coding more than the body so he thinks of transforming the rank of the matrix. So he can do transformation as well as coding.
You will be given a matrix ‘mat’ of size ‘M * N’. Your task is to return a new matrix ‘ANSWER’ where ‘ANSWER[row][col]’ is the rank of ‘MATRIX[row][col]’.
For calculating the rank of the matrix following rule must be followed:
The rank is an integer starting from 1.
If two elements p and q are in the same row or column, then:
If p < q then rank(p) < rank(q)
If p == q then rank(p) == rank(q)
If p > q then rank(p) > rank(q)
The rank should be as small as possible.
Example:
Suppose the matrix is [ [ 1, 2 ], [ 3, 4 ] ] so we return rank as [ [ 1, 2 ], [ 2, 3 ] ] as
The rank of MATRIX[0][0] is 1 because it is the smallest integer in its row and column.
The rank of MATRIX[0][1] is 2 because MATRIX[0][1] > MATRIX[0][0] and MATRIX[0][0] is rank 1.
The rank of MATRIX[1][0] is 2 because MATRIX[1][0] > MATRIX[0][0] and MATRIX[0][0] is rank 1.
The rank of MATRIX[1][1] is 3 because MATRIX[1][1] > MATRIX[0][1], MATRIX[1][1] > MATRIX[1][0], and both MATRIX[0][1] and MATRIX[1][0] are rank 2.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow:
Each test case’s first line contains two space-separated integers, ‘N’ and ‘M’, denoting the number of rows and columns in the matrix.
The next ‘N’ lines contain ‘M’ integers that denoting the elements of the MATRIX.
Output format:
For every test case, print 'N' lines each line containing 'M' space-seprated integers denoting the elements of the new matrix containing the rank of the matrix.
The output of each test case will be printed a separate line.
Note:
You are not required to print anything explicitly. It has already been taken care of just implement the function.
1 <= T <= 10
1 <= N, M <= 100
-10 ^ 5 <= MATRIX[i][j] <= 10^5
Where ‘T’ is the number of test cases, ‘N’ is the number of rows, and ‘M' is the number of columns in a 'MATRIX'.
Time limit: 1 second
2
2 2
1 2
3 4
2 2
7 7
7 7
1 2
2 3
1 1
1 1
Test Case 1:
For this test case, we calculate the rank of the matrix in this way:
The rank of matrix[0][0] is 1 because it is the smallest integer in its row and column.
The rank of matrix[0][1] is 2 because matrix[0][1] > matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][0] is 2 because matrix[1][0] > matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][1] is 3 because matrix[1][1] > matrix[0][1], matrix[1][1] > matrix[1][0], and both matrix[0][1] and matrix[1][0] are rank 2.
Test Case 2:
For this test case as all elements are equals so the rank of each index is ‘1’.
2
4 3
20 -21 14
-19 4 19
22 -47 24
-19 4 19
3 3
7 3 6
1 4 5
9 8 2
4 2 3
1 3 4
5 1 6
1 3 4
5 1 4
1 2 3
6 3 1
Can you think of a union-find?
The idea here is to process values from the smallest to largest, and track the current rank for each row of rows[i] and column col[j]. Because our values are sorted, ranks will always increase for the next value. For each group, we need to identify a rank, which is a maximum rank (plus one) of all included rows and columns.
O((N * M) * log(N * M), where ‘N’ is the number of rows and ‘M’ is the number of columns in the matrix.
We are sorting our matrix and then comparing it for calculating the rank that takes O((N * M) * log(N * M) time. Hence the overall time complexity is O((N * M) * log(N * M).
O(N), where ‘N’ is the number of rows in the matrix.
As we are using a set for storing them in pairs that takes O(N) space. Hence the overall space complexity is O(N).