File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } grid
3+ * @return {number }
4+ */
5+ var minimumOperationsToWriteY = function ( grid ) {
6+ const mapY = new Map ( )
7+ const mapNotY = new Map ( )
8+ const n = grid . length
9+ for ( let i = 0 ; i < n ; i ++ ) {
10+ for ( let j = 0 ; j < n ; j ++ ) {
11+ let inY = false
12+ if ( i <= n / 2 ) {
13+ if ( i === j || i + j === n - 1 ) {
14+ inY = true
15+ }
16+ } else {
17+ if ( j === Math . floor ( n / 2 ) ) {
18+ inY = true
19+ }
20+ }
21+
22+ const item = grid [ i ] [ j ]
23+ if ( inY ) {
24+ mapY . set ( item , ( mapY . get ( item ) || 0 ) + 1 )
25+ } else {
26+ mapNotY . set ( item , ( mapNotY . get ( item ) || 0 ) + 1 )
27+ }
28+ }
29+ }
30+ const countY = n + Math . floor ( n / 2 )
31+ const countNotY = n * n - countY
32+ let res = Infinity
33+ for ( let a = 0 ; a <= 2 ; a ++ ) {
34+ for ( let b = 0 ; b <= 2 ; b ++ ) {
35+ if ( a === b ) continue
36+ const tmp1 = mapY . get ( a ) || 0
37+ const tmp2 = mapNotY . get ( b ) || 0
38+
39+ const tmp = countY - tmp1 + ( countNotY - tmp2 )
40+ res = Math . min ( res , tmp )
41+ }
42+ }
43+ return res
44+ }
You can’t perform that action at this time.
0 commit comments