↓Skip to main content
  1. LeetCode/

LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid

·2 mins· ·
LeetCode Medium Array Hash-Table Matrix Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2024-03-03

  • Submissions: Accepted (first try)

Approach
#

The Y has three segments: top-left to center, top-right to center, and center down to the bottom (Matrix Template 3). Count the 0s, 1s and 2s on and off the Y (Hash Table), then over the 6 pairs (a, b) with a != b the changes needed are |Y| - Y[a] + |rest| - rest[b].

My Attempts
#

Passed on the first try.

Solution
#

class Solution:
    def minimumOperationsToWriteY(self, grid: List[List[int]]) -> int:
        n = len(grid)
        mid = n // 2
        on, off = [0] * 3, [0] * 3
        for i in range(n):
            for j in range(n):
                in_y = (i <= mid and (j == i or j == n - 1 - i)) or (i >= mid and j == mid)
                (on if in_y else off)[grid[i][j]] += 1
        total_on, total_off = sum(on), sum(off)
        return min(total_on - on[a] + total_off - off[b]
                   for a in range(3) for b in range(3) if a != b)

Complexity
#

Time O(n²), space O(1).

Mistakes to Avoid
#

  • The center belongs to all three segments; count it once.
  • The Y and background values must differ.

Interview Explanation
#

I count each value on and off the Y, then over the six pairs of distinct Y and background values the cost is the cells not already matching, and I take the minimum. Time O(n²).

Related

LeetCode 229: Majority Element II
·2 mins
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 note: at most two values can exceed n / 3; Boyer-Moore keeps two candidates and a second pass verifies their counts
LeetCode 73: Set Matrix Zeroes
·2 mins
LeetCode Medium Array Hash-Table Matrix
LeetCode 73 note: record which rows and columns contain a 0 first, then update afterward; the follow-up uses the first row and column as markers
LeetCode 36: Valid Sudoku
·2 mins
LeetCode Medium Array Hash-Table Matrix
LeetCode 36 note: each digit may appear once per row, column and 3 × 3 box; put (digit, row), (digit, column) and (digit, box) keys into one set
LeetCode 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·2 mins
LeetCode Medium Array Matrix Prefix-Sum
LeetCode 3070 note: submatrices containing the top-left corner are exactly the 2D prefix sums P[i][j]; count those <= k
LeetCode 240: Search a 2D Matrix II
·2 mins
LeetCode Medium Array Binary-Search Divide-and-Conquer Matrix
LeetCode 240 note: start at the top-right corner, moving left when larger and down when smaller, eliminating a row or column each step
LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 106 note: the last post-order value is the root; taking roots from the end of the post-order, build the right subtree before the left