
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.
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.
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.
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
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.
The idea here is to use the same idea above but instead of sorting, we use a map for storing them from smallest to the largest.