Given a 2D grid of size m x n, you should find the matrix answer of size
m x n.
The cell answer[r][c] is calculated by looking at the diagonal values of the cell grid[r][c]:
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.
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.
Then answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|.
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.
For example, in the below diagram the diagonal is highlighted using the cell with indices (2, 3) colored gray:
For each cell, the diagonal to the left and above is all cells (r-1, c-1), (r-2, c-2), … and the diagonal to the right and below is all cells (r+1, c+1), (r+2, c+2), … We can precompute the distinct values for each diagonal in both directions using hash sets.
classSolution {
publicint[][]differenceOfDistinctValues(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] left =newint[m][n], right =newint[m][n];
for (int d =-(n-1); d <= m-1; ++d) {
Set<Integer> s =new HashSet<>();
for (int i = Math.max(0, d), j = i - d; i < m && j < n; ++i, ++j) {
left[i][j]= s.size();
s.add(grid[i][j]);
}
}
for (int d =-(n-1); d <= m-1; ++d) {
Set<Integer> s =new HashSet<>();
List<int[]> cells =new ArrayList<>();
for (int i = Math.max(0, d), j = i - d; i < m && j < n; ++i, ++j)
cells.add(newint[]{i, j});
for (int k = cells.size()-1; k >= 0; --k) {
int i = cells.get(k)[0], j = cells.get(k)[1];
right[i][j]= s.size();
s.add(grid[i][j]);
}
}
int[][] ans =newint[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ans[i][j]= Math.abs(left[i][j]- right[i][j]);
return ans;
}
}
classSolution {
fundifferenceOfDistinctValues(grid: Array<IntArray>): Array<IntArray> {
val m = grid.size
val n = grid[0].size
val left = Array(m) { IntArray(n) }
val right = Array(m) { IntArray(n) }
for (d in -(n-1)..(m-1)) {
val s = mutableSetOf<Int>()
var i = maxOf(0, d)
var j = i - d
while (i < m && j < n) {
left[i][j] = s.size
s.add(grid[i][j])
i++ j++ }
}
for (d in -(n-1)..(m-1)) {
val s = mutableSetOf<Int>()
val cells = mutableListOf<Pair<Int, Int>>()
var i = maxOf(0, d)
var j = i - d
while (i < m && j < n) {
cells.add(i to j)
i++ j++ }
for (k in cells.size-1 downTo 0) {
val(ii, jj) = cells[k]
right[ii][jj] = s.size
s.add(grid[ii][jj])
}
}
val ans = Array(m) { IntArray(n) }
for (i in0 until m)
for (j in0 until n)
ans[i][j] = kotlin.math.abs(left[i][j] - right[i][j])
return ans
}
}
classSolution:
defdifferenceOfDistinctValues(self, grid: list[list[int]]) -> list[list[int]]:
m, n = len(grid), len(grid[0])
left = [[0]*n for _ in range(m)]
right = [[0]*n for _ in range(m)]
for d in range(-(n-1), m):
s = set()
i, j = max(0, d), max(0, -d)
while i < m and j < n:
left[i][j] = len(s)
s.add(grid[i][j])
i +=1 j +=1for d in range(-(n-1), m):
s = set()
cells = []
i, j = max(0, d), max(0, -d)
while i < m and j < n:
cells.append((i, j))
i +=1 j +=1for i, j in reversed(cells):
right[i][j] = len(s)
s.add(grid[i][j])
ans = [[abs(left[i][j] - right[i][j]) for j in range(n)] for i in range(m)]
return ans
impl Solution {
pubfndifference_of_distinct_values(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let m = grid.len();
let n = grid[0].len();
letmut left =vec![vec![0; n]; m];
letmut right =vec![vec![0; n]; m];
for d in-(n asisize-1)..=(m asisize-1) {
letmut s = std::collections::HashSet::new();
letmut i = std::cmp::max(0, d) asusize;
letmut j = (i asisize- d) asusize;
while i < m && j < n {
left[i][j] = s.len() asi32;
s.insert(grid[i][j]);
i +=1;
j +=1;
}
}
for d in-(n asisize-1)..=(m asisize-1) {
letmut s = std::collections::HashSet::new();
letmut cells =vec![];
letmut i = std::cmp::max(0, d) asusize;
letmut j = (i asisize- d) asusize;
while i < m && j < n {
cells.push((i, j));
i +=1;
j +=1;
}
for&(i, j) in cells.iter().rev() {
right[i][j] = s.len() asi32;
s.insert(grid[i][j]);
}
}
letmut ans =vec![vec![0; n]; m];
for i in0..m {
for j in0..n {
ans[i][j] = (left[i][j] - right[i][j]).abs();
}
}
ans
}
}