2711. Difference of Number of Distinct Values on Diagonals Medium
1/**
2 * [2711] Difference of Number of Distinct Values on Diagonals
3 *
4 * Given a 2D grid of size m x n, you should find the matrix answer of size m x n.
5 * The cell answer[r][c] is calculated by looking at the diagonal values of the cell grid[r][c]:
6 *
7 * Let leftAbove[r][c] be the number of distinct values on the diagonal to the left and above the cell grid[r][c] not including the cell grid[r][c] itself.
8 * Let rightBelow[r][c] be the number of distinct values on the diagonal to the right and below the cell grid[r][c], not including the cell grid[r][c] itself.
9 * Then answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|.
10 *
11 * A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until the end of the matrix is reached.
12 *
13 * For example, in the below diagram the diagonal is highlighted using the cell with indices (2, 3) colored gray:
14 *
15 * Red-colored cells are left and above the cell.
16 * Blue-colored cells are right and below the cell.
17 *
18 *
19 *
20 * <img alt="" src="https://assets.leetcode.com/uploads/2024/05/26/diagonal.png" style="width: 200px; height: 160px;" />
21 * Return the matrix answer.
22 *
23 * <strong class="example">Example 1:
24 * <div class="example-block">
25 * Input: <span class="example-io">grid = [[1,2,3],[3,1,5],[3,2,1]]</span>
26 * Output: <span class="example-io">Output: [[1,1,0],[1,0,1],[0,1,1]]</span>
27 * Explanation:
28 * To calculate the answer cells:
29 * <table>
30 * <thead>
31 * <tr>
32 * <th>answer</th>
33 * <th>left-above elements</th>
34 * <th>leftAbove</th>
35 * <th>right-below elements</th>
36 * <th>rightBelow</th>
37 * <th>|leftAbove - rightBelow|</th>
38 * </tr>
39 * </thead>
40 * <tbody>
41 * <tr>
42 * <td>[0][0]</td>
43 * <td>[]</td>
44 * <td>0</td>
45 * <td>[grid[1][1], grid[2][2]]</td>
46 * <td>|{1, 1}| = 1</td>
47 * <td>1</td>
48 * </tr>
49 * <tr>
50 * <td>[0][1]</td>
51 * <td>[]</td>
52 * <td>0</td>
53 * <td>[grid[1][2]]</td>
54 * <td>|{5}| = 1</td>
55 * <td>1</td>
56 * </tr>
57 * <tr>
58 * <td>[0][2]</td>
59 * <td>[]</td>
60 * <td>0</td>
61 * <td>[]</td>
62 * <td>0</td>
63 * <td>0</td>
64 * </tr>
65 * <tr>
66 * <td>[1][0]</td>
67 * <td>[]</td>
68 * <td>0</td>
69 * <td>[grid[2][1]]</td>
70 * <td>|{2}| = 1</td>
71 * <td>1</td>
72 * </tr>
73 * <tr>
74 * <td>[1][1]</td>
75 * <td>[grid[0][0]]</td>
76 * <td>|{1}| = 1</td>
77 * <td>[grid[2][2]]</td>
78 * <td>|{1}| = 1</td>
79 * <td>0</td>
80 * </tr>
81 * <tr>
82 * <td>[1][2]</td>
83 * <td>[grid[0][1]]</td>
84 * <td>|{2}| = 1</td>
85 * <td>[]</td>
86 * <td>0</td>
87 * <td>1</td>
88 * </tr>
89 * <tr>
90 * <td>[2][0]</td>
91 * <td>[]</td>
92 * <td>0</td>
93 * <td>[]</td>
94 * <td>0</td>
95 * <td>0</td>
96 * </tr>
97 * <tr>
98 * <td>[2][1]</td>
99 * <td>[grid[1][0]]</td>
100 * <td>|{3}| = 1</td>
101 * <td>[]</td>
102 * <td>0</td>
103 * <td>1</td>
104 * </tr>
105 * <tr>
106 * <td>[2][2]</td>
107 * <td>[grid[0][0], grid[1][1]]</td>
108 * <td>|{1, 1}| = 1</td>
109 * <td>[]</td>
110 * <td>0</td>
111 * <td>1</td>
112 * </tr>
113 * </tbody>
114 * </table>
115 * </div>
116 * <strong class="example">Example 2:
117 * <div class="example-block">
118 * Input: <span class="example-io">grid = [[1]]</span>
119 * Output: <span class="example-io">Output: [[0]]</span>
120 * </div>
121 *
122 * Constraints:
123 *
124 * m == grid.length
125 * n == grid[i].length
126 * 1 <= m, n, grid[i][j] <= 50
127 *
128 */
129pub struct Solution {}
130
131// problem: https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/
132// discuss: https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/discuss/?currentPage=1&orderBy=most_votes&query=
133
134// submission codes start here
135
136impl Solution {
137 pub fn difference_of_distinct_values(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
138 vec![]
139 }
140}
141
142// submission codes end
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn test_2711() {
150 }
151}
152
Back
© 2025 bowen.ge All Rights Reserved.