forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximize-area-of-square-hole-in-grid.cpp
51 lines (47 loc) · 1.56 KB
/
maximize-area-of-square-hole-in-grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Time: O(h + v), h = len(hBars), v = len(vBars)
// Space: O(h + v)
// array, hash table
class 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, sort
class 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;
}
};