leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2751.cpp (1165B)


0 class Solution {
1 public:
2 vector<int> survivedRobotsHealths(const vector<int> &positions, vector<int> &healths,
3 const string &directions) const {
4 static int indexes[100001];
5 const int n = size(positions);
6 vector<int> res;
7 stack<int> st;
9 iota(indexes, indexes + n, 0);
10 sort(indexes, indexes + n, [&](int a, int b) { return positions[a] < positions[b]; });
12 for (const int idx : std::span(indexes, n)) {
13 if (directions[idx] == 'R') {
14 st.push(idx);
15 continue;
16 }
18 int &health = healths[idx];
19 while (!st.empty() && health > 0) {
20 int &top = healths[st.top()];
21 if (health == top)
22 top = health = 0, st.pop();
23 else if (health > top)
24 health--, top = 0, st.pop();
25 else
26 top--, health = 0;
27 }
28 }
30 for (int i = 0; i < n; i++) {
31 if (healths[i] == 0) continue;
32 res.push_back(healths[i]);
33 }
35 return res;
36 }
37 };