3071. Minimum Operations to Write the Letter Y on a Grid Medium
1/**
2 * [3071] Minimum Operations to Write the Letter Y on a Grid
3 *
4 * You are given a 0-indexed n x n grid where n is odd, and grid[r][c] is 0, 1, or 2.
5 * We say that a cell belongs to the Letter Y if it belongs to one of the following:
6 *
7 * The diagonal starting at the top-left cell and ending at the center cell of the grid.
8 * The diagonal starting at the top-right cell and ending at the center cell of the grid.
9 * The vertical line starting at the center cell and ending at the bottom border of the grid.
10 *
11 * The Letter Y is written on the grid if and only if:
12 *
13 * All values at cells belonging to the Y are equal.
14 * All values at cells not belonging to the Y are equal.
15 * The values at cells belonging to the Y are different from the values at cells not belonging to the Y.
16 *
17 * 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.
18 *
19 * <strong class="example">Example 1:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2024/01/22/y2.png" style="width: 461px; height: 121px;" />
21 * Input: grid = [[1,2,2],[1,1,0],[0,1,0]]
22 * Output: 3
23 * Explanation: We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0.
24 * It can be shown that 3 is the minimum number of operations needed to write Y on the grid.
25 *
26 * <strong class="example">Example 2:
27 * <img alt="" src="https://assets.leetcode.com/uploads/2024/01/22/y3.png" style="width: 701px; height: 201px;" />
28 * Input: grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]
29 * Output: 12
30 * Explanation: We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2.
31 * It can be shown that 12 is the minimum number of operations needed to write Y on the grid.
32 *
33 * Constraints:
34 *
35 * 3 <= n <= 49
36 * n == grid.length == grid[i].length
37 * 0 <= grid[i][j] <= 2
38 * n is odd.
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/minimum-operations-to-write-the-letter-y-on-a-grid/
44// discuss: https://leetcode.com/problems/minimum-operations-to-write-the-letter-y-on-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn minimum_operations_to_write_y(grid: Vec<Vec<i32>>) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_3071() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.