Problem solution · C++

Maximize Area of Square Hole in Grid

Maximize Area of Square Hole in Grid: a C++ solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Binary search
Source
Kamyu LeetCode Solutions
Length
52 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Maximize Area of Square Hole in Grid, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 52 lines of C++ from the credited upstream file maximize-area-of-square-hole-in-grid.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 5 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximize Area of Square Hole in Grid · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(h + v), h = len(hBars), v = len(vBars)// Space: O(h + v) // array, hash tableclass Solution {public:    int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {        const auto& max_gap = [&](const auto& arr) {            int result = 1;            unordered_set<int> lookup(cbegin(arr), cend(arr));            while (!empty(lookup)) {                const int x = *begin(lookup);                int left = x;                for (; lookup.count(left - 1); --left);                int right = x;                for (; lookup.count(right + 1); ++right);                for (int i = left; i <= right; ++i) {                    lookup.erase(i);                }                result = max(result, (right - left + 1) + 1);            }            return result;        };         const int l = min(max_gap(hBars), max_gap(vBars));        return l * l;    }}; // Time:  O(hlogh + vlogv), h = len(hBars), v = len(vBars)// Space: O(1)// array, sortclass Solution2 {public:    int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {        const auto& max_gap = [&](auto& arr) {            sort(begin(arr), end(arr));            int result = 1;            for (int i = 0, l = 1; i < size(arr); ++i) {                result = max(result, ++l);                if (i + 1 < size(arr) && arr[i + 1] != arr[i] + 1) {                    l = 1;                }            }            return result;        };         const int l = min(max_gap(hBars), max_gap(vBars));        return l * l;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗