You are given the two integers, n and m and two integer arrays, hBars and vBars. The grid has n + 2 horizontal and m + 2 vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from 1.
You can remove some of the bars in hBars from horizontal bars and some of the bars in vBars from vertical bars. Note that other bars are fixed and cannot be removed.
Return an integer denoting the maximum area of a square-shaped hole in the grid, after removing some bars (possibly none).
Example 1:

Input: n = 2, m = 1, hBars = [2,3], vBars = [2]
Output: 4
Explanation:
The left image shows the initial grid formed by the bars. The horizontal bars are [1,2,3,4], and the vertical bars are [1,2,3].
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
Example 2:

Input: n = 1, m = 1, hBars = [2], vBars = [2]
Output: 4
Explanation:
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
Example 3:

Input: n = 2, m = 3, hBars = [2,3], vBars = [2,4]
Output: 4
Explanation:
One way to get the maximum square-shaped hole is by removing horizontal bar 3, and vertical bar 4.
Constraints:
1 <= n <= 1091 <= m <= 1091 <= hBars.length <= 1002 <= hBars[i] <= n + 11 <= vBars.length <= 1002 <= vBars[i] <= m + 1hBars are distinct.vBars are distinct.This approach utilizes a recursive DFS algorithm. DFS can efficiently explore possible solutions to problems like searching tree or graph structures. Through recursion, the algorithm will delve deep into each node before backtracking, making it suitable for problems requiring exploration of all potential paths.
The Python DFS function uses recursion to visit nodes. It starts with a node, visits it if it hasn't been already, and then recursively visits each of its neighbors. A set is used to keep track of visited nodes to avoid cycles.
C++
Time Complexity: O(V + E) where V is the number of vertices and E the number of edges.
Space Complexity: O(V) to store visited vertices in the worst case.
The BFS iterative approach uses a queue to manage nodes. It explores a neighbor before moving deeper, which helps in finding the shortest path or level-wise processing. This method is beneficial in scenarios where all nodes are needed before moving levels.
This Java BFS method uses a queue to track and visit nodes level by level. It ensures no node is visited more than once by using a set, making it efficient for graph and tree-like data structures.
JavaScript
Time Complexity: O(V + E) where V is the number of vertices and E the number of edges.
Space Complexity: O(V) due to the queue and set for storing visited nodes.
| Approach | Complexity |
|---|---|
| Recursive Depth-First Search (DFS) | Time Complexity: O(V + E) where V is the number of vertices and E the number of edges. |
| Iterative Breadth-First Search (BFS) | Time Complexity: O(V + E) where V is the number of vertices and E the number of edges. |
LARGEST RECTANGLE IN HISTOGRAM - Leetcode 84 - Python • NeetCode • 287,868 views views
Watch 9 more video solutions →Practice Maximize Area of Square Hole in Grid with our built-in code editor and test cases.
Practice on FleetCode