-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3071-minimum-operations-to-write-the-letter-y-on-a-grid.js
More file actions
63 lines (56 loc) · 2.08 KB
/
3071-minimum-operations-to-write-the-letter-y-on-a-grid.js
File metadata and controls
63 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 3071. Minimum Operations to Write the Letter Y on a Grid
* https://leetcode.com/problems/minimum-operations-to-write-the-letter-y-on-a-grid/
* Difficulty: Medium
*
* You are given a 0-indexed n x n grid where n is odd, and grid[r][c] is 0, 1, or 2.
*
* We say that a cell belongs to the Letter Y if it belongs to one of the following:
* - The diagonal starting at the top-left cell and ending at the center cell of the grid.
* - The diagonal starting at the top-right cell and ending at the center cell of the grid.
* - The vertical line starting at the center cell and ending at the bottom border of the grid.
*
* The Letter Y is written on the grid if and only if:
* - All values at cells belonging to the Y are equal.
* - All values at cells not belonging to the Y are equal.
* - The values at cells belonging to the Y are different from the values at cells not belonging
* to the Y.
*
* Return the minimum number of operations needed to write the letter Y on the grid given that
* in one operation you can change the value at any cell to 0, 1, or 2.
*/
/**
* @param {number[][]} grid
* @return {number}
*/
var minimumOperationsToWriteY = function(grid) {
const n = grid.length;
const center = Math.floor(n / 2);
const yCells = [];
const nonYCells = [];
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if ((i === j && i <= center) || (i === n - 1 - j && i <= center)
|| (j === center && i >= center)) {
yCells.push(grid[i][j]);
} else {
nonYCells.push(grid[i][j]);
}
}
}
const yCounts = [0, 0, 0];
const nonYCounts = [0, 0, 0];
for (const val of yCells) yCounts[val]++;
for (const val of nonYCells) nonYCounts[val]++;
let minOperations = Infinity;
for (let yVal = 0; yVal <= 2; yVal++) {
for (let nonYVal = 0; nonYVal <= 2; nonYVal++) {
if (yVal !== nonYVal) {
const yOps = yCells.length - yCounts[yVal];
const nonYOps = nonYCells.length - nonYCounts[nonYVal];
minOperations = Math.min(minOperations, yOps + nonYOps);
}
}
}
return minOperations;
};