-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-total-area-occupied-by-pistons.cpp
60 lines (58 loc) · 2.05 KB
/
maximum-total-area-occupied-by-pistons.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
// Time: O(h)
// Space: O(h)
// line sweep, difference array
class Solution {
public:
long long maxArea(int height, vector<int>& positions, string directions) {
vector<int> diff(2 * height + 1);
for (int i = 0; i < size(positions); ++i) {
if (directions[i] == 'U') {
--diff[height - positions[i]];
++diff[(height - positions[i]) + height];
} else {
++diff[positions[i]];
--diff[positions[i] + height];
}
}
int64_t total = accumulate(cbegin(positions), cend(positions), 0ll);
int64_t result = total;
int cnt = count(cbegin(directions), cend(directions), 'U');
for (int t = 1; t < size(diff); ++t) {
total += -(static_cast<int32_t>(size(directions)) - cnt) + cnt;
result = max(result, total);
cnt += diff[t];
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// sort, line sweep, difference array
class Solution2 {
public:
long long maxArea(int height, vector<int>& positions, string directions) {
unordered_map<int, int> diff;
for (int i = 0; i < size(positions); ++i) {
if (directions[i] == 'U') {
--diff[height - positions[i]];
++diff[(height - positions[i]) + height];
} else {
++diff[positions[i]];
--diff[positions[i] + height];
}
}
int64_t total = accumulate(cbegin(positions), cend(positions), 0ll);
int64_t result = total;
int cnt = count(cbegin(directions), cend(directions), 'U');
int prev = 0;
vector<pair<int, int>> sorted_diff(cbegin(diff), cend(diff));
sort(begin(sorted_diff), end(sorted_diff));
for (const auto& [t, d] : sorted_diff) {
total += static_cast<int64_t>(t - prev) * (-(static_cast<int32_t>(size(directions)) - cnt) + cnt);
result = max(result, total);
cnt += d;
prev = t;
}
return result;
}
};