File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } grid
3
+ * @return {number }
4
+ */
5
+ const matrixScore = function ( grid ) {
6
+ const m = grid . length , n = grid [ 0 ] . length
7
+ for ( let i = 0 ; i < m ; i ++ ) {
8
+ if ( grid [ i ] [ 0 ] === 0 ) flipRow ( i )
9
+ }
10
+
11
+ for ( let i = 0 ; i < n ; i ++ ) {
12
+ if ( cntCol ( i , 0 ) > cntCol ( i , 1 ) ) flipCol ( i )
13
+ }
14
+
15
+ let res = 0
16
+ // console.log(grid)
17
+ for ( const row of grid ) {
18
+ res += parseInt ( row . join ( '' ) , 2 )
19
+ }
20
+
21
+ return res
22
+
23
+
24
+ function flipRow ( idx ) {
25
+ for ( let i = 0 ; i < n ; i ++ ) {
26
+ if ( grid [ idx ] [ i ] === 0 ) grid [ idx ] [ i ] = 1
27
+ else grid [ idx ] [ i ] = 0
28
+ }
29
+ }
30
+
31
+ function cntCol ( idx , target ) {
32
+ let res = 0
33
+ for ( let i = 0 ; i < m ; i ++ ) {
34
+ if ( grid [ i ] [ idx ] === target ) res ++
35
+ }
36
+ // console.log(res)
37
+ return res
38
+ }
39
+
40
+ function flipCol ( idx ) {
41
+ for ( let i = 0 ; i < m ; i ++ ) {
42
+ if ( grid [ i ] [ idx ] === 0 ) grid [ i ] [ idx ] = 1
43
+ else grid [ i ] [ idx ] = 0
44
+ }
45
+ }
46
+ } ;
You can’t perform that action at this time.
0 commit comments