Minimum Operations to Write the Letter Y on a Grid - Problem

You are given a 0-indexed n × 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.

Input & Output

Example 1 — Basic 3x3 Grid
$ Input: grid = [[1,2,2],[1,1,0],[0,1,0]]
Output: 3
💡 Note: Y cells are at positions (0,0), (0,2), (1,1), (2,1). To minimize operations, set Y=1 and non-Y=0. Need to change 3 non-Y cells: (0,1) from 2→0, (1,0) from 1→0, (1,2) from 0→0 (no change), (2,0) from 0→0, (2,2) from 0→0. Actually need to change (0,1) and (1,0) and (1,2), total 3 operations.
Example 2 — Uniform Values
$ Input: grid = [[2,1,0],[1,0,1],[0,1,2]]
Output: 6
💡 Note: Y cells: (0,0)=2, (0,2)=0, (1,1)=0, (2,1)=1. Non-Y cells: (0,1)=1, (1,0)=1, (1,2)=1, (2,0)=0, (2,2)=2. Best assignment requires changing most cells to achieve uniform regions.
Example 3 — Already Optimal
$ Input: grid = [[0,1,0],[1,0,1],[1,0,1]]
Output: 1
💡 Note: Y cells: (0,0)=0, (0,2)=0, (1,1)=0, (2,1)=0. Non-Y: (0,1)=1, (1,0)=1, (1,2)=1, (2,0)=1, (2,2)=1. Y=0, Non-Y=1 is already mostly correct, need only 1 change.

Constraints

  • n == grid.length == grid[i].length
  • 3 ≤ n ≤ 49
  • n is odd
  • grid[i][j] is either 0, 1, or 2

Visualization

Tap to expand
Minimum Operations to Write Letter Y on Grid INPUT 1 2 2 1 1 0 0 1 0 Y cells Non-Y cells Input Grid: [[1,2,2], [1,1,0], [0,1,0]] n = 3 (odd) ALGORITHM STEPS 1 Identify Y Cells Diagonals + vertical stem 2 Count Values Count 0,1,2 in Y and non-Y 3 Try All Combinations 6 pairs: (0,1)(0,2)(1,0)... 4 Find Minimum Min operations needed Value Counts: Y cells: {1:3, 2:2} Non-Y: {0:3, 1:1} Y=1, nonY=0: 2+1=3 ops Y=1, nonY=2: 2+4=6 ops Y=2, nonY=0: 3+1=4 ops Y=0, nonY=1: 5+3=8 ops Min = 3 operations FINAL RESULT 1 0 1 0 1 0 0 1 0 Y=1 Non-Y=0 Output: 3 Changes: (0,1)2-->0 (0,2)2-->1, (1,2)0-->0 OK - Valid Y! Key Insight: The Y pattern consists of 2 diagonals meeting at center + vertical stem down. Try all 6 valid (Y_value, nonY_value) combinations where Y_value != nonY_value. For each combo, count cells needing change. Return minimum. Time: O(n^2) to count values, O(1) to check 6 combinations. Space: O(1) for counters. TutorialsPoint - Minimum Operations to Write the Letter Y on a Grid | Optimal Solution
Asked in
Google 12 Meta 8 Amazon 6
9.0K Views
Medium Frequency
~15 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen