-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2751. Robot Collisions.cpp
41 lines (38 loc) · 1.26 KB
/
2751. Robot Collisions.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
class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
stack<int> st;
vector<int> sorted_indices(positions.size());
iota(sorted_indices.begin(), sorted_indices.end(), 0);
sort(sorted_indices.begin(), sorted_indices.end(), [&positions](int i, int j) {
return positions[i] < positions[j];
});
for (int i : sorted_indices) {
if (directions[i] == 'R') {
st.push(i);
} else {
while (!st.empty() && healths[st.top()] < healths[i]) {
healths[i] -= 1;
healths[st.top()] = 0;
st.pop();
}
if (!st.empty()) {
if (healths[st.top()] == healths[i]) {
healths[st.top()] = 0;
st.pop();
} else {
healths[st.top()] -= 1;
}
healths[i] = 0;
}
}
}
vector<int> res;
for (int health : healths) {
if (health > 0) {
res.push_back(health);
}
}
return res;
}
};