Skip to content

Commit e319489

Browse files
authored
Create 3071-minimum-operations-to-write-the-letter-y-on-a-grid.js
1 parent 18c326d commit e319489

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

0 commit comments

Comments
 (0)