NewThe Learning module is now liveSee what's new

(This question is a variation of the LeetCode question 2943. Maximize Area of Square Hole in Grid. If you haven't completed that question yet, it is recommended to solve it first.)

A prisoner is planning to escape from prison. The gate consists of n horizontal bars and m vertical bars, equally spaced one unit apart, forming a grid where every opening has an initial area of 1 × 1. The prisoner can remove certain bars to create larger openings.

Given n, m, and the 1-indexed indices of bars to remove (h for horizontal bars and v for vertical bars), return the area of the largest hole in the gate after all specified bars have been removed.

Constraints:

  • 1 ≤ n, m ≤ 104
  • 1 ≤ h.length ≤ n
  • 1 ≤ v.length ≤ m
  • 1 ≤ h[i] ≤ n
  • 1 ≤ v[j] ≤ m
  • All values in h are distinct.
  • All values in v are distinct.

Example 1:

Input: n = 6, m = 6, h = [4], v = [2] Output: 4 Explanation:

  • Removing horizontal bar 4 creates a vertical gap of height 2 between the remaining bars at positions 3 and 5.
  • Removing vertical bar 2 creates a horizontal gap of width 2 between the remaining bars at positions 1 and 3.

The largest hole is 2 × 2 = 4.

Example 2:

Input: n = 2, m = 2, h = [1], v = [2] Output: 4

Example 3:

Input: n = 5, m = 5, h = [4, 2, 3], v = [3] Output: 8


Accepted
35/67
Acceptance Rate52.2%
Test Cases
Input
n =
m =
h =
v =
Output
Expected