The Rank Transform of a Matrix problem asks you to assign a rank to each cell: the smallest element gets rank 1, tied elements in the same row or column must get the same rank, and ranks must be strictly greater than ranks of smaller elements in the same row or column. This hard coding problem uses Union-Find with sorted element processing. The array, matrix, union find, graph, sorting, and topological sort interview pattern is comprehensively tested.
Citadel, Meta, and Google ask this hard problem because it requires combining sorting with Union-Find to handle ties across rows and columns. Elements must be grouped by value, then within each group, cells sharing a row or column must receive the same rank (taking the maximum constraint from their group).
Sort by value + Union-Find per value group. Sort all cells by value. Process groups of equal-value cells: union cells sharing a row or column within the same group. For each component, the rank = max(current_rank_in_row_or_col) + 1 across all cells in the component. Update row and column rank arrays.
Matrix=[[1,2],[3,4]]. All distinct: (0,0)→1, (0,1)→2, (1,0)→3, (1,1)→4.
Rank Transform of a Matrix combines Union-Find with value-based grouping. The algorithm has three phases: (1) sort all cells by value, (2) process each value group with Union-Find, (3) assign ranks based on component constraints. Practice this three-phase pattern on similar "ranked assignment with group constraints" problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimize Maximum Value in a Grid | Hard | Solve | |
| Build a Matrix With Conditions | Hard | Solve | |
| Strange Printer II | Hard | Solve | |
| Checking Existence of Edge Length Limited Paths | Hard | Solve | |
| Number of Good Paths | Hard | Solve |