Approach
We find the longest consecutive in both dimension, and take the minimum one out of two dimension, and that is the best square width.
Code
class Solution {
public:
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
int side = min(longestConsecutivePlus1(hBars), longestConsecutivePlus1(vBars));
return side * side;
}
static int longestConsecutivePlus1(vector<int>& a) {
if (a.empty()) return 1;
sort(a.begin(), a.end());
int best = 1;
int run = 1;
for (int i = 1; i < a.size(); ++i) {
if (a[i] == a[i-1] + 1) ++run;
else run = 1;
best = max(best, run);
}
return best + 1;
}
};