-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmaximum-walls-destroyed-by-robots.cpp
More file actions
42 lines (40 loc) · 1.7 KB
/
maximum-walls-destroyed-by-robots.cpp
File metadata and controls
42 lines (40 loc) · 1.7 KB
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
// Time: O(nlogn + mlogm)
// Space: O(n)
// sort, dp, two pointers
class Solution {
public:
int maxWalls(vector<int>& robots, vector<int>& distance, vector<int>& walls) {
static const int INF = numeric_limits<int>::max();
vector<pair<int, int>> x_d;
x_d.emplace_back(0, 0);
for (int i = 0; i < size(robots); ++i) {
x_d.emplace_back(robots[i], distance[i]);
}
x_d.emplace_back(INF, 0);
sort(begin(x_d), end(x_d), [](const auto& a, const auto& b) {
return a.first < b.first;
});
sort(begin(walls), end(walls));
int left0 = 0, left1 = 0, right = 0, curr = 0;
vector<int> dp(2), new_dp(2);
for (int i = 1; i + 1 < size(x_d); ++i) {
for (; curr < size(walls) && walls[curr] < x_d[i].first; ++curr);
const auto& r = min(x_d[i].first + x_d[i].second, x_d[i + 1].first - 1);
for (; right < size(walls) && walls[right] <= r; ++right);
new_dp[1] = max(dp[0], dp[1]) + (right - curr);
if (curr < size(walls) && walls[curr] == x_d[i].first) {
++curr;
}
const auto& l0 = max(x_d[i].first - x_d[i].second, x_d[i - 1].first + 1);
for (; left0 < size(walls) && walls[left0] < l0; ++left0);
const auto& l1 = max(
min(x_d[i - 1].first + x_d[i - 1].second, x_d[i].first - 1) + 1,
max(x_d[i].first - x_d[i].second, x_d[i - 1].first + 1)
);
for (; left1 < size(walls) && walls[left1] < l1; ++left1);
new_dp[0] = max(dp[0] + (curr - left0), dp[1] + (curr - left1));
swap(dp, new_dp);
}
return max(dp[0], dp[1]);
}
};