Problem Description
Given an m x n matrix, transform it into a new matrix "answer" such that answer[row][col] is the rank of matrix[row][col]. The rank is defined by the following criteria:
- 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 ranks should be as small as possible. It is guaranteed that the answer is unique under these rules.
Key Insights
- Process the matrix cells in increasing order of their values.
- Group cells with the same value that are in the same row or column using a union–find (disjoint set union) structure.
- Maintain a rank array for each row and column. When processing a group, determine the new rank as the maximum current rank in that group plus one.
- Update the rank for each involved row and column and assign the computed rank to the answer matrix.
- This ensures that the rank assignment maintains all the given constraints.
Space and Time Complexity
Time Complexity: O(m * n * α(m+n)) where α is the inverse Ackermann function (almost constant) – the dominant overhead is sorting all cells. Space Complexity: O(m * n) due to storing rank and union–find structures as well as grouping cells.
Solution
The solution uses a union–find (DSU) data structure to group cells with the same value from the same row or column. We first map every unique value to a list of its cell positions then process these values in sorted order. For each group:
- Create a DSU for m+n nodes where nodes 0…m-1 represent rows and nodes m…(m+n-1) represent columns.
- For every cell in the group, union the node corresponding to the row with the node corresponding to the column (offset by m).
- For each unioned component, determine the maximum rank from the current rows and columns and assign new_rank = max + 1.
- Update the answer matrix for each cell of the current value and also update the rank for the corresponding row and column. This ensures that when processing subsequent cells, the new rank respects the ordering conditions among rows and columns.
Code Solutions
Below are code implementations with detailed inline comments in Python, JavaScript, C++, and Java.