Assign Minimum Consistent Ranks to Matrix Cells
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests greedy assignment of ranks by processing elements in sorted order while maintaining row and column constraints.
Given an m x n matrix containing distinct integers, assign each cell a rank (positive integer) following these rules:
- Ranks start at
1 - Within any row or column, a cell holding a larger value must receive a strictly larger rank than cells with smaller values
- Subject to the above constraints, every rank should be as small as possible
Return a matrix of the same dimensions where each cell contains its assigned rank.
Since all values in the input are distinct, there is exactly one valid ranking.
Constraints
1 <= m, n <= 500m * n <= 10^5-10^9 <= matrix[i][j] <= 10^9- All values in the matrix are distinct
Examples
Example 1
Input: assign_ranks([[10, 40, 20, 30]])
Output: [[1, 4, 2, 3]]
Example 2
Input: assign_ranks([[1, 5], [4, 2]])
Output: [[1, 2], [2, 1]]
Example 3
Input: assign_ranks([[3, 1, 9], [8, 2, 7], [4, 6, 5]])
Output: [[2, 1, 6], [6, 2, 5], [3, 5, 4]]
Hints
Hint 1
Consider processing cells in sorted order by their values. What information do you need to track?
Hint 2
For each row and column, track the highest rank assigned so far. When you process a cell, how does this help determine its rank?
Hint 3
Because values are distinct and processed in increasing order, the most recent rank in a row or column is always the maximum for that row or column.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to process cells in increasing order of their values. When we assign a rank to a cell, all smaller values in its row and column have already been processed.
For each cell at position (r, c), its rank must be:
- Greater than every smaller value in row
r - Greater than every smaller value in column
c
We maintain two arrays tracking the highest rank assigned so far in each row and column. When processing a cell, its minimum valid rank is one more than the maximum of these two values.
Why does tracking only the most recent rank work? Because we process in sorted order, ranks within any single row or column form a strictly increasing sequence. Therefore, the most recently assigned rank in a row or column is automatically the maximum for that row or column.
Correctness
Constraint satisfaction: Each cell gets a rank exactly one higher than the maximum among all smaller values in its row and column, ensuring the strict ordering requirement.
Minimality: By induction on processing order: assume all previously processed (smaller) cells have minimal ranks. The current cell cannot receive a rank smaller than max(row_max[r], col_max[c]) + 1 without violating a constraint. Since we assign exactly this value, the assignment is minimal.
Complexity
- Time: O(mn log(mn)) dominated by sorting all cells. Processing the sorted cells takes O(mn).
- Space: O(mn) for the output matrix and sorted cell list, plus O(m + n) for the tracking arrays.
Solution
from typing import List
def assign_ranks(matrix: List[List[int]]) -> List[List[int]]:
m = len(matrix)
n = len(matrix[0])
# Sort all cells by value
cells = sorted((matrix[r][c], r, c) for r in range(m) for c in range(n))
# Track the highest rank assigned in each row and column
row_max = [0] * m
col_max = [0] * n
result = [[0] * n for _ in range(m)]
for _, r, c in cells:
# This cell must rank higher than all smaller values in its row/column
rank = max(row_max[r], col_max[c]) + 1
result[r][c] = rank
row_max[r] = rank
col_max[c] = rank
return result