Skip to content

Commit cd8ccf5

Browse files
committed
feat: add js solution to lc problem: No.2751
1 parent dab7068 commit cd8ccf5

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

solution/2700-2799/2751.Robot Collisions/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,51 @@ function survivedRobotsHealths(
315315
}
316316
```
317317

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+
318363
<!-- tabs:end -->
319364

320365
<!-- solution:end -->

solution/2700-2799/2751.Robot Collisions/README_EN.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,51 @@ function survivedRobotsHealths(
315315
}
316316
```
317317

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+
318363
<!-- tabs:end -->
319364

320365
<!-- solution:end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
};

0 commit comments

Comments
 (0)