Rank Transform of a Matrix - Problem

You are given an m × n matrix and need to transform it into a rank matrix where each element is replaced by its relative ranking position.

The ranking system follows these critical rules:

  • Ranks start from 1 (not 0)
  • Row and column constraints: If two elements are in the same row or column, their relative order must be preserved (smaller value gets smaller rank)
  • Equal elements in the same row/column get the same rank
  • Minimize ranks: Use the smallest possible rank values

For example, in matrix [[1,2],[3,4]], element 1 gets rank 1, element 2 gets rank 2, but element 3 must get rank 2 (not 3) because it's only larger than elements in its row/column, and element 4 gets rank 3.

Challenge: The tricky part is handling the interconnected constraints across rows and columns simultaneously!

Input & Output

example_1.py — Simple 2x2 Matrix
$ Input: matrix = [[1,2],[3,4]]
Output: [[1,2],[2,3]]
💡 Note: Element 1 (smallest) gets rank 1. Element 2 is larger than 1 in its row, so rank 2. Element 3 is larger than 1 in its column, so rank 2. Element 4 is larger than elements 2 and 3 in its row/column, so rank 3.
example_2.py — Matrix with Equal Elements
$ Input: matrix = [[7,7],[7,7]]
Output: [[1,1],[1,1]]
💡 Note: All elements are equal, so they all get the same rank 1. This is the minimum possible rank since there are no smaller elements.
example_3.py — Complex Constraints
$ Input: matrix = [[20,-21,14],[-19,4,19],[22,-47,-24]]
Output: [[4,2,3],[1,3,4],[5,1,2]]
💡 Note: This demonstrates complex row/column dependencies. Element -47 is smallest globally (rank 1), but other elements' ranks depend on their row and column constraints, creating an interconnected ranking system.

Visualization

Tap to expand
🏆 Matrix Rank Transform TournamentOriginal Matrix:1234Rank Matrix:1223Processing Order:1. Value 1 → Rank 12. Value 2 → Rank 23. Value 3 → Rank 24. Value 4 → Rank 3Row & Column Constraints:• Row 1: 1 < 2, so rank(1) < rank(2) ✓ (1 < 2)• Row 2: 3 < 4, so rank(3) < rank(4) ✓ (2 < 3)• Col 1: 1 < 3, so rank(1) < rank(3) ✓ (1 < 2)• Col 2: 2 < 4, so rank(2) < rank(4) ✓ (2 < 3)Algorithm Steps:1. Group cells by value: {1:[0,0], 2:[0,1], 3:[1,0], 4:[1,1]}2. Use Union-Find for same-value same-row/col cells3. Process values in ascending order: 1,2,3,44. For each value group: rank = max(row_ranks, col_ranks) + 15. Update row_ranks and col_ranks after assignment🎯 Key Insight: Process equal elements together while respecting row/column ordering!Time: O(mn×α(mn)) | Space: O(mn) | α = inverse Ackermann function
Understanding the Visualization
1
Group Players by Skill
Collect all matrix positions that have the same value - these players have equal skill
2
Connect Team/Division Members
Use Union-Find to connect equal-skilled players who are in the same row (team) or column (division)
3
Process by Skill Level
Handle skill groups from lowest to highest, ensuring proper ranking order
4
Assign Tournament Ranks
For each connected group, assign the next available rank based on existing team and division rankings
Key Takeaway
🎯 Key Insight: The Union-Find approach elegantly handles the interconnected row and column constraints by processing equal-valued elements as connected components in ascending value order.

Time & Space Complexity

Time Complexity
⏱️
O(mn × α(mn))

We process each cell once, and Union-Find operations take O(α(mn)) amortized time where α is the inverse Ackermann function

n
2n
Linear Growth
Space Complexity
O(mn)

Space for Union-Find structure, row/column rank tracking, and grouping elements by value

n
2n
Linearithmic Space

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 ≤ m, n ≤ 500
  • -109 ≤ matrix[row][col] ≤ 109
  • The test cases are generated so that answer is unique under the given rules
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22
23.4K Views
Medium Frequency
~25 min Avg. Time
986 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen