Skip to content

Commit a76c48a

Browse files
authored
Create 2768-number-of-black-blocks.js
1 parent b2f4f91 commit a76c48a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

2768-number-of-black-blocks.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
let arr = new Array(5).fill(0)
9+
arr[0] = (m - 1) * (n - 1)
10+
let mat = {}
11+
for (let [r, c] of coordinates) {
12+
mat[r] ||= []
13+
mat[r][c] = true
14+
}
15+
for (let [r, c] of coordinates) {
16+
for (let i = -1; i < 1; i++) {
17+
for (let j = -1; j < 1; j++) {
18+
let nextR = r + i
19+
let nextC = c + j
20+
if (nextR < 0 || nextC < 0 || (nextR >= m - 1) | (nextC >= n - 1))
21+
continue
22+
let res = getRes(nextR, nextC, mat)
23+
arr[res]++
24+
}
25+
}
26+
}
27+
for (let i = 1; i < 5; i++) {
28+
arr[i] = ~~(arr[i] / i)
29+
}
30+
let used = 0
31+
for (let i = 1; i < 5; i++) {
32+
used += arr[i]
33+
}
34+
arr[0] -= used
35+
return arr
36+
}
37+
38+
const getRes = (r, c, mat) => {
39+
let res = 0
40+
for (let i = 0; i < 2; i++) {
41+
for (let j = 0; j < 2; j++) {
42+
let nextR = r + i
43+
let nextC = c + j
44+
if (mat[nextR]?.[nextC] === true) res++
45+
}
46+
}
47+
return res
48+
}

0 commit comments

Comments
 (0)