@@ -90,25 +90,187 @@ tags:
90
90
#### Python3
91
91
92
92
``` python
93
+ class Solution :
94
+ def survivedRobotsHealths (
95
+ self , positions : List[int ], healths : List[int ], directions : str
96
+ ) -> List[int ]:
97
+ n = len (positions)
98
+ indices = list (range (n))
99
+ stack = []
100
+
101
+ indices.sort(key = lambda i : positions[i])
102
+
103
+ for currentIndex in indices:
104
+ if directions[currentIndex] == " R" :
105
+ stack.append(currentIndex)
106
+ else :
107
+ while stack and healths[currentIndex] > 0 :
108
+ topIndex = stack.pop()
109
+
110
+ if healths[topIndex] > healths[currentIndex]:
111
+ healths[topIndex] -= 1
112
+ healths[currentIndex] = 0
113
+ stack.append(topIndex)
114
+ elif healths[topIndex] < healths[currentIndex]:
115
+ healths[currentIndex] -= 1
116
+ healths[topIndex] = 0
117
+ else :
118
+ healths[currentIndex] = 0
119
+ healths[topIndex] = 0
120
+
121
+ result = [health for health in healths if health > 0 ]
122
+ return result
93
123
94
124
```
95
125
96
126
#### Java
97
127
98
128
``` java
99
-
129
+ class Solution {
130
+ public List<Integer > survivedRobotsHealths (int [] positions , int [] healths , String directions ) {
131
+ int n = positions. length;
132
+ Integer [] indices = new Integer [n];
133
+ for (int i = 0 ; i < n; i++ ) {
134
+ indices[i] = i;
135
+ }
136
+
137
+ Arrays . sort(indices, (i, j) - > Integer . compare(positions[i], positions[j]));
138
+
139
+ Stack<Integer > stack = new Stack<> ();
140
+
141
+ for (int currentIndex : indices) {
142
+ if (directions. charAt(currentIndex) == ' R' ) {
143
+ stack. push(currentIndex);
144
+ } else {
145
+ while (! stack. isEmpty() && healths[currentIndex] > 0 ) {
146
+ int topIndex = stack. pop();
147
+
148
+ if (healths[topIndex] > healths[currentIndex]) {
149
+ healths[topIndex] -= 1 ;
150
+ healths[currentIndex] = 0 ;
151
+ stack. push(topIndex);
152
+ } else if (healths[topIndex] < healths[currentIndex]) {
153
+ healths[currentIndex] -= 1 ;
154
+ healths[topIndex] = 0 ;
155
+ } else {
156
+ healths[currentIndex] = 0 ;
157
+ healths[topIndex] = 0 ;
158
+ }
159
+ }
160
+ }
161
+ }
162
+
163
+ List<Integer > result = new ArrayList<> ();
164
+ for (int health : healths) {
165
+ if (health > 0 ) {
166
+ result. add(health);
167
+ }
168
+ }
169
+
170
+ return result;
171
+ }
172
+ }
100
173
```
101
174
102
175
#### C++
103
176
104
177
``` cpp
105
-
178
+ class Solution {
179
+ public:
180
+ vector<int > survivedRobotsHealths(vector<int >& positions,
181
+ vector<int >& healths, string directions) {
182
+ int n = positions.size();
183
+ vector<int > indices(n);
184
+
185
+ iota(indices.begin(), indices.end(), 0);
186
+ stack<int> st;
187
+
188
+ auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };
189
+
190
+ sort(begin(indices), end(indices), lambda);
191
+
192
+ vector<int> result;
193
+ for (int currentIndex : indices) {
194
+ if (directions[currentIndex] == 'R') {
195
+ st.push(currentIndex);
196
+ } else {
197
+ while (!st.empty() && healths[currentIndex] > 0) {
198
+ int topIndex = st.top();
199
+ st.pop();
200
+
201
+ if (healths[topIndex] > healths[currentIndex]) {
202
+ healths[topIndex] -= 1;
203
+ healths[currentIndex] = 0;
204
+ st.push(topIndex);
205
+ } else if (healths[topIndex] < healths[currentIndex]) {
206
+ healths[currentIndex] -= 1;
207
+ healths[topIndex] = 0;
208
+ } else {
209
+ healths[currentIndex] = 0;
210
+ healths[topIndex] = 0;
211
+ }
212
+ }
213
+ }
214
+ }
215
+
216
+ for (int i = 0 ; i < n; ++i) {
217
+ if (healths[i] > 0) {
218
+ result.push_back(healths[i]);
219
+ }
220
+ }
221
+ return result;
222
+ }
223
+ };
106
224
```
107
225
108
226
#### Go
109
227
110
228
``` go
111
-
229
+ func survivedRobotsHealths (positions []int , healths []int , directions string ) []int {
230
+ n := len (positions)
231
+ indices := make ([]int , n)
232
+ for i := range indices {
233
+ indices[i] = i
234
+ }
235
+
236
+ sort.Slice (indices, func (i, j int ) bool {
237
+ return positions[indices[i]] < positions[indices[j]]
238
+ })
239
+
240
+ stack := []int {}
241
+
242
+ for _ , currentIndex := range indices {
243
+ if directions[currentIndex] == ' R' {
244
+ stack = append (stack, currentIndex)
245
+ } else {
246
+ for len (stack) > 0 && healths[currentIndex] > 0 {
247
+ topIndex := stack[len (stack)-1 ]
248
+ stack = stack[:len (stack)-1 ]
249
+
250
+ if healths[topIndex] > healths[currentIndex] {
251
+ healths[topIndex] -= 1
252
+ healths[currentIndex] = 0
253
+ stack = append (stack, topIndex)
254
+ } else if healths[topIndex] < healths[currentIndex] {
255
+ healths[currentIndex] -= 1
256
+ healths[topIndex] = 0
257
+ } else {
258
+ healths[currentIndex] = 0
259
+ healths[topIndex] = 0
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ result := []int {}
266
+ for _ , health := range healths {
267
+ if health > 0 {
268
+ result = append (result, health)
269
+ }
270
+ }
271
+
272
+ return result
273
+ }
112
274
```
113
275
114
276
<!-- tabs:end -->
0 commit comments