1632. Rank Transform of a Matrix Hard

@problem@discussion
#Array#Greedy#Union Find#Graph#Topological Sort#Matrix



1/**
2 * [1632] Rank Transform of a Matrix
3 *
4 * Given an m x n matrix, return a new matrix answer where answer[row][col] is the rank of matrix[row][col].
5 * The rank is an integer that represents how large an element is compared to other elements. It is calculated using the following rules:
6 * 
7 * 	The rank is an integer starting from 1.
8 * 	If two elements p and q are in the same row or column, then:
9 * 	
10 * 		If p < q then rank(p) < rank(q)
11 * 		If p == q then rank(p) == rank(q)
12 * 		If p > q then rank(p) > rank(q)
13 * 	
14 * 	
15 * 	The rank should be as small as possible.
16 * 
17 * The test cases are generated so that answer is unique under the given rules.
18 *  
19 * Example 1:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/18/rank1.jpg" style="width: 442px; height: 162px;" />
21 * Input: matrix = [[1,2],[3,4]]
22 * Output: [[1,2],[2,3]]
23 * Explanation:
24 * The rank of matrix[0][0] is 1 because it is the smallest integer in its row and column.
25 * The rank of matrix[0][1] is 2 because matrix[0][1] > matrix[0][0] and matrix[0][0] is rank 1.
26 * The rank of matrix[1][0] is 2 because matrix[1][0] > matrix[0][0] and matrix[0][0] is rank 1.
27 * 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.
28 * 
29 * Example 2:
30 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/18/rank2.jpg" style="width: 442px; height: 162px;" />
31 * Input: matrix = [[7,7],[7,7]]
32 * Output: [[1,1],[1,1]]
33 * 
34 * Example 3:
35 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/18/rank3.jpg" style="width: 601px; height: 322px;" />
36 * Input: matrix = [[20,-21,14],[-19,4,19],[22,-47,24],[-19,4,19]]
37 * Output: [[4,2,3],[1,3,4],[5,1,6],[1,3,4]]
38 * 
39 *  
40 * Constraints:
41 * 
42 * 	m == matrix.length
43 * 	n == matrix[i].length
44 * 	1 <= m, n <= 500
45 * 	-10^9 <= matrix[row][col] <= 10^9
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/rank-transform-of-a-matrix/
51// discuss: https://leetcode.com/problems/rank-transform-of-a-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn matrix_rank_transform(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
57        vec![]
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_1632() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.