-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-area-rectangle-with-point-constraints-ii.cpp
70 lines (64 loc) · 1.9 KB
/
maximum-area-rectangle-with-point-constraints-ii.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
68
69
70
// Time: O(nlogn)
// Space: O(n)
// sort, fenwick tree, hash table
class BIT {
public:
BIT(int n) : bit_(n + 1) { // 0-indexed
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
class Solution {
public:
long long maxRectangleArea(vector<int>& xCoord, vector<int>& yCoord) {
vector<pair<int, int>> points;
for (int i = 0; i < size(xCoord); ++i) {
points.emplace_back(xCoord[i], yCoord[i]);
}
sort(begin(points), end(points));
set<int> sorted_ys;
for (const auto& [_, y] : points) {
sorted_ys.emplace(y);
}
unordered_map<int, int> y_to_idx;
for (const auto& y : sorted_ys) {
y_to_idx[y] = size(y_to_idx);
}
BIT bit(size(y_to_idx));
unordered_map<int, unordered_map<int, pair<int, int>>> lookup;
int64_t result = -1;
int prev_x = -1, prev_y = -1;
for (const auto& [x, y] : points) {
const int y_idx = y_to_idx[y];
bit.add(y_idx, +1);
if (x == prev_x) {
const int prev_y_idx = y_to_idx[prev_y];
const int curr = bit.query(y_idx) - bit.query(prev_y_idx - 1);
const auto [prev, prev_x2] = lookup[prev_y_idx][y_idx];
if (prev && prev == curr - 2) {
result = max(result, static_cast<int64_t>(x - prev_x2) * (y - prev_y));
}
lookup[prev_y_idx][y_idx] = pair(curr, x);
}
prev_x = x, prev_y = y;
}
return result;
}
};