-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-square-area-by-removing-fences-from-a-field.cpp
67 lines (61 loc) · 2.04 KB
/
maximum-square-area-by-removing-fences-from-a-field.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Time: O(h^2 + v^2)
// Space: O(min(h, v)^2)
// hash table
class Solution {
public:
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
static const int MOD = 1e9 + 7;
int64_t result = -1;
unordered_set<int64_t> lookup;
const auto& diff = [&](auto& arr, int x, bool check) {
arr.emplace_back(1);
arr.emplace_back(x);
for (int i = 0; i < size(arr); ++i) {
for (int j = i + 1; j < size(arr); ++j) {
if (!check) {
lookup.insert(abs(arr[i] - arr[j])); // use insert rather than emplace to speed up
continue;
}
if (lookup.count(abs(arr[i] - arr[j]))) {
result = max(result, static_cast<int64_t>(arr[i] - arr[j]) * (arr[i] - arr[j]));
}
}
}
};
if (size(hFences) > size(vFences)) {
swap(hFences, vFences);
swap(m, n);
}
diff(hFences, m, false);
diff(vFences, n, true);
return result % MOD;
}
};
// Time: O(h^2 + v^2)
// Space: O(h^2 + v^2)
// hash table
class Solution2 {
public:
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
static const int MOD = 1e9 + 7;
const auto& diff = [](auto& arr, int x) {
unordered_set<int64_t> lookup;
arr.emplace_back(1);
arr.emplace_back(x);
for (int i = 0; i < size(arr); ++i) {
for (int j = i + 1; j < size(arr); ++j) {
lookup.insert(abs(arr[i] - arr[j])); // use insert rather than emplace to speed up
}
}
return lookup;
};
const auto& lookup = diff(hFences, m);
int64_t result = -1;
for (const auto& x : diff(vFences, n)) {
if (lookup.count(x)) {
result = max(result, x * x);
}
}
return result % MOD;
}
};