-
Notifications
You must be signed in to change notification settings - Fork 0
/
countBeautiful.cpp
84 lines (68 loc) · 2.05 KB
/
countBeautiful.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
struct Robot {
int position;
int health;
int index;
};
vector<int> survivingRobots(vector<int>& positions, vector<int>& healths, string& directions) {
int n = positions.size();
vector<Robot> robots(n);
// Create a list of robots with position, health, and index
for (int i = 0; i < n; i++) {
robots[i].position = positions[i];
robots[i].health = healths[i];
robots[i].index = i;
}
// Sort the robots based on their positions
sort(robots.begin(), robots.end(), [](const Robot& a, const Robot& b) {
return a.position < b.position;
});
stack<Robot> stk;
for (const Robot& robot : robots) {
int position = robot.position;
int health = robot.health;
int index = robot.index;
while (!stk.empty()) {
Robot prevRobot = stk.top();
int prevPosition = prevRobot.position;
int prevHealth = prevRobot.health;
int prevIndex = prevRobot.index;
if (prevPosition == position) {
if (prevHealth <= health) {
stk.pop();
if (prevHealth < health) {
health--;
}
} else {
break;
}
} else {
break;
}
}
stk.push({position, health, index});
}
vector<int> finalHealths;
while (!stk.empty()) {
finalHealths.push_back(stk.top().health);
stk.pop();
}
// Reverse the order to match the original indices
reverse(finalHealths.begin(), finalHealths.end());
return finalHealths;
}
int main() {
vector<int> positions = {3,5,2,6};
vector<int> healths = {10,10,15,12};
string directions = "RLRL";
vector<int> finalHealths = survivingRobots(positions, healths, directions);
for (int health : finalHealths) {
cout << health << " ";
}
cout << endl;
return 0;
}