Skip to content

Commit 8365b8f

Browse files
authored
Update 2768-number-of-black-blocks.js
1 parent a76c48a commit 8365b8f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

2768-number-of-black-blocks.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
1+
/**
2+
* @param {number} m
3+
* @param {number} n
4+
* @param {number[][]} coordinates
5+
* @return {number[]}
6+
*/
7+
const countBlackBlocks = function(m, n, coordinates) {
8+
const block = {}
9+
10+
for(const [i, j] of coordinates) {
11+
helper(i, j)
12+
}
13+
14+
const arr = Array(5).fill(0)
15+
for(const k in block) {
16+
if(block.hasOwnProperty(k)) {
17+
arr[block[k]]++
18+
}
19+
}
20+
arr[0] = (m - 1) * (n - 1) - arr.reduce((ac, e) => ac + e, 0)
21+
return arr
22+
23+
function helper(i, j) {
24+
25+
if(i < m - 1 && j < n - 1) {
26+
update(i, j)
27+
if(i > 0) update(i - 1, j)
28+
if(j > 0) update(i, j - 1)
29+
if(i > 0 && j > 0) update(i - 1, j - 1)
30+
} else if(i === m - 1 && j === n - 1) {
31+
update(i - 1, j - 1)
32+
} else if(i === m - 1) {
33+
update(i - 1, j)
34+
if(j > 0) update(i - 1, j - 1)
35+
} else if(j === n - 1) {
36+
update(i, j - 1)
37+
if(i > 0) update(i - 1, j - 1)
38+
}
39+
40+
}
41+
42+
function update(i, j, delta = 1) {
43+
const key = `${i},${j}`
44+
if(block[key] == null) block[key] = 0
45+
block[key]++
46+
}
47+
};
48+
49+
// another
50+
151
/**
252
* @param {number} m
353
* @param {number} n

0 commit comments

Comments
 (0)