LeetCode solutions

3214. Maximize Area of Square Hole in Grid

My accepted Python solution to LeetCode problem 3214, Maximize Area of Square Hole in Grid, running in 3ms.

  • Difficulty: Medium
  • Python
  • Runtime 3ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.4MB, accepted 2026-01-01.

python
class Solution:
    def maximizeSquareHoleArea(self, n: int, m: int, hBars: List[int], vBars: List[int]) -> int:
        def maxConsecutive(bars):
            if not bars:
                return 1
            bars.sort()
            max_len = 1
            curr_len = 1
            for i in range(1, len(bars)):
                if bars[i] == bars[i-1] + 1:
                    curr_len += 1
                else:
                    curr_len = 1
                max_len = max(max_len, curr_len)
            return max_len + 1
        
        h_max = maxConsecutive(hBars)
        v_max = maxConsecutive(vBars)
        side = min(h_max, v_max)
        return side * side

Source