-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot Collisions.cpp
39 lines (34 loc) · 1.14 KB
/
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
class Solution {
public:
using int2=pair<int, int>;// (position, idx)
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string& directions) {
const int n=positions.size();
vector<int2> robot(n);
for(int i=0; i<n; i++)// 0-indexed is fine
robot[i]={positions[i], i};
sort(robot.begin(), robot.end(), greater<>());
vector<int> stack;
for(auto& [pos, i]: robot){
if (directions[i]=='L') stack.push_back(i);
else{
while(!stack.empty() && healths[i]>0){
int j=stack.back();
int x=healths[j]-healths[i];
if (x>0) healths[j]--, healths[i]=0;
else if (x<0) healths[j]=0, healths[i]--, stack.pop_back();
else healths[i]=healths[j]=0, stack.pop_back();
}
}
}
vector<int> ans;
for(int x: healths)
if (x>0) ans.push_back(x);
return ans;
}
};
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();