File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-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
+ 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
+
1
51
/**
2
52
* @param {number } m
3
53
* @param {number } n
You can’t perform that action at this time.
0 commit comments