Skip to content

Commit f094dab

Browse files
authored
Create 2711-difference-of-number-of-distinct-values-on-diagonals.js
1 parent f249742 commit f094dab

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number[][]}
4+
*/
5+
const differenceOfDistinctValues = function (grid) {
6+
const m = grid.length,
7+
n = grid[0].length,
8+
{ abs } = Math
9+
const res = Array.from({ length: m }, () => Array(n).fill(0))
10+
for (let i = 0; i < m; i++) {
11+
for (let j = 0; j < n; j++) {
12+
const bottomRight = new Set()
13+
const topLeft = new Set()
14+
let x = i + 1
15+
let y = j + 1
16+
while (x >= 0 && x < m && y >= 0 && y < n) {
17+
bottomRight.add(grid[x][y])
18+
x++
19+
y++
20+
}
21+
x = i - 1
22+
y = j - 1
23+
while (x >= 0 && x < m && y >= 0 && y < n) {
24+
topLeft.add(grid[x][y])
25+
x--
26+
y--
27+
}
28+
29+
res[i][j] = abs(bottomRight.size - topLeft.size)
30+
}
31+
}
32+
return res
33+
}

0 commit comments

Comments
 (0)