File tree Expand file tree Collapse file tree 3 files changed +130
-0
lines changed
solution/2700-2799/2751.Robot Collisions Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change @@ -315,6 +315,51 @@ function survivedRobotsHealths(
315
315
}
316
316
```
317
317
318
+ #### JavaScript
319
+
320
+ ``` js
321
+ /**
322
+ * @param {number[]} positions
323
+ * @param {number[]} healths
324
+ * @param {string} directions
325
+ * @return {number[]}
326
+ */
327
+ var survivedRobotsHealths = function (positions , healths , directions ) {
328
+ const idx = Array .from ({ length: positions .length }, (_ , i ) => i);
329
+ const stk = [];
330
+
331
+ idx .sort ((a , b ) => positions[a] - positions[b]);
332
+
333
+ for (let iRight of idx) {
334
+ while (stk .length ) {
335
+ const iLeft = stk .at (- 1 );
336
+ const havePair = directions[iLeft] === ' R' && directions[iRight] === ' L' ;
337
+ if (! havePair) break ;
338
+
339
+ if (healths[iLeft] === healths[iRight]) {
340
+ healths[iLeft] = healths[iRight] = iRight = - 1 ;
341
+ stk .pop ();
342
+ break ;
343
+ }
344
+
345
+ if (healths[iLeft] < healths[iRight]) {
346
+ healths[iLeft] = - 1 ;
347
+ healths[iRight]-- ;
348
+ stk .pop ();
349
+ } else {
350
+ healths[iRight] = iRight = - 1 ;
351
+ healths[iLeft]-- ;
352
+ break ;
353
+ }
354
+ }
355
+
356
+ if (iRight !== - 1 ) stk .push (iRight);
357
+ }
358
+
359
+ return healths .filter (i => ~ i);
360
+ };
361
+ ```
362
+
318
363
<!-- tabs:end -->
319
364
320
365
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -315,6 +315,51 @@ function survivedRobotsHealths(
315
315
}
316
316
```
317
317
318
+ #### JavaScript
319
+
320
+ ``` js
321
+ /**
322
+ * @param {number[]} positions
323
+ * @param {number[]} healths
324
+ * @param {string} directions
325
+ * @return {number[]}
326
+ */
327
+ var survivedRobotsHealths = function (positions , healths , directions ) {
328
+ const idx = Array .from ({ length: positions .length }, (_ , i ) => i);
329
+ const stk = [];
330
+
331
+ idx .sort ((a , b ) => positions[a] - positions[b]);
332
+
333
+ for (let iRight of idx) {
334
+ while (stk .length ) {
335
+ const iLeft = stk .at (- 1 );
336
+ const havePair = directions[iLeft] === ' R' && directions[iRight] === ' L' ;
337
+ if (! havePair) break ;
338
+
339
+ if (healths[iLeft] === healths[iRight]) {
340
+ healths[iLeft] = healths[iRight] = iRight = - 1 ;
341
+ stk .pop ();
342
+ break ;
343
+ }
344
+
345
+ if (healths[iLeft] < healths[iRight]) {
346
+ healths[iLeft] = - 1 ;
347
+ healths[iRight]-- ;
348
+ stk .pop ();
349
+ } else {
350
+ healths[iRight] = iRight = - 1 ;
351
+ healths[iLeft]-- ;
352
+ break ;
353
+ }
354
+ }
355
+
356
+ if (iRight !== - 1 ) stk .push (iRight);
357
+ }
358
+
359
+ return healths .filter (i => ~ i);
360
+ };
361
+ ```
362
+
318
363
<!-- tabs:end -->
319
364
320
365
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } positions
3
+ * @param {number[] } healths
4
+ * @param {string } directions
5
+ * @return {number[] }
6
+ */
7
+ var survivedRobotsHealths = function ( positions , healths , directions ) {
8
+ const idx = Array . from ( { length : positions . length } , ( _ , i ) => i ) ;
9
+ const stk = [ ] ;
10
+
11
+ idx . sort ( ( a , b ) => positions [ a ] - positions [ b ] ) ;
12
+
13
+ for ( let iRight of idx ) {
14
+ while ( stk . length ) {
15
+ const iLeft = stk . at ( - 1 ) ;
16
+ const havePair = directions [ iLeft ] === 'R' && directions [ iRight ] === 'L' ;
17
+ if ( ! havePair ) break ;
18
+
19
+ if ( healths [ iLeft ] === healths [ iRight ] ) {
20
+ healths [ iLeft ] = healths [ iRight ] = iRight = - 1 ;
21
+ stk . pop ( ) ;
22
+ break ;
23
+ }
24
+
25
+ if ( healths [ iLeft ] < healths [ iRight ] ) {
26
+ healths [ iLeft ] = - 1 ;
27
+ healths [ iRight ] -- ;
28
+ stk . pop ( ) ;
29
+ } else {
30
+ healths [ iRight ] = iRight = - 1 ;
31
+ healths [ iLeft ] -- ;
32
+ break ;
33
+ }
34
+ }
35
+
36
+ if ( iRight !== - 1 ) stk . push ( iRight ) ;
37
+ }
38
+
39
+ return healths . filter ( i => ~ i ) ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments