Skip to content

2943. Maximize Area of Square Hole in Grid

  • Time: $O(\texttt{sort}(\texttt{hBars}) + \texttt{sort}(\texttt{vBars}))$
  • Space: $O(\texttt{sort}(\texttt{hBars}) + \texttt{sort}(\texttt{vBars}))$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
 public:
  int maximizeSquareHoleArea(int n, int m, vector<int>& hBars,
                             vector<int>& vBars) {
    const int gap = min(maxContinousGap(hBars), maxContinousGap(vBars));
    return gap * gap;
  }

 private:
  int maxContinousGap(vector<int>& bars) {
    int res = 2;
    int runningGap = 2;
    ranges::sort(bars);
    for (int i = 1; i < bars.size(); ++i) {
      runningGap = bars[i] == bars[i - 1] + 1 ? runningGap + 1 : 2;
      res = max(res, runningGap);
    }
    return res;
  }
};