-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2751.cpp
56 lines (55 loc) · 1.95 KB
/
2751.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
class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
int n = positions.size();
unordered_map<int, int> pos2idx;
vector<tuple<int, int, char>> combined;
for (int i = 0; i < n; ++i) {
combined.push_back(make_tuple(positions[i], healths[i], directions[i]));
pos2idx[positions[i]] = i;
}
sort(combined.begin(), combined.end());
stack<tuple<int, int, char>> st;
for (int i = 0; i < n; ++i) {
auto [pos, health, dir] = combined[i];
if (dir == 'R') {
st.push(make_tuple(pos, health, dir));
}
else if (st.empty() || get<2>(st.top()) == 'L') {
st.push(make_tuple(pos, health, dir));
}
else {
bool canPush = true;
while (!st.empty() && get<2>(st.top()) == 'R') {
auto [topPos, topHealth, topDir] = st.top();
st.pop();
if (topHealth == health) {
canPush = false;
break;
}
if (topHealth > health) {
canPush = false;
st.push(make_tuple(topPos, topHealth - 1, topDir));
break;
}
else {
health -= 1;
}
}
if (canPush) st.push(make_tuple(pos, health, dir));
}
}
vector<pair<int, int>> temp;
while (!st.empty()) {
auto [topPos, topHealth, topDir] = st.top();
st.pop();
temp.push_back({pos2idx[topPos], topHealth});
}
sort(temp.begin(), temp.end());
vector<int> res;
for (auto& [idx, health] : temp) {
res.push_back(health);
}
return res;
}
};