File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments