Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60ea283

Browse files
authored
feat: add js solution to lc problem: No.2751 (#3313)
1 parent 445cbf5 commit 60ea283

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,94 @@ func survivedRobotsHealths(positions []int, healths []int, directions string) []
272272
}
273273
```
274274

275+
#### TypeScript
276+
277+
```ts
278+
function survivedRobotsHealths(
279+
positions: number[],
280+
healths: number[],
281+
directions: string,
282+
): number[] {
283+
const idx = Array.from({ length: positions.length }, (_, i) => i);
284+
const stk: number[] = [];
285+
286+
idx.sort((a, b) => positions[a] - positions[b]);
287+
288+
for (let iRight of idx) {
289+
while (stk.length) {
290+
const iLeft = stk.at(-1)!;
291+
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
292+
if (!havePair) break;
293+
294+
if (healths[iLeft] === healths[iRight]) {
295+
healths[iLeft] = healths[iRight] = iRight = -1;
296+
stk.pop();
297+
break;
298+
}
299+
300+
if (healths[iLeft] < healths[iRight]) {
301+
healths[iLeft] = -1;
302+
healths[iRight]--;
303+
stk.pop();
304+
} else {
305+
healths[iRight] = iRight = -1;
306+
healths[iLeft]--;
307+
break;
308+
}
309+
}
310+
311+
if (iRight !== -1) stk.push(iRight);
312+
}
313+
314+
return healths.filter(i => ~i);
315+
}
316+
```
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+
275363
<!-- tabs:end -->
276364

277365
<!-- solution:end -->

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,94 @@ func survivedRobotsHealths(positions []int, healths []int, directions string) []
272272
}
273273
```
274274

275+
#### TypeScript
276+
277+
```ts
278+
function survivedRobotsHealths(
279+
positions: number[],
280+
healths: number[],
281+
directions: string,
282+
): number[] {
283+
const idx = Array.from({ length: positions.length }, (_, i) => i);
284+
const stk: number[] = [];
285+
286+
idx.sort((a, b) => positions[a] - positions[b]);
287+
288+
for (let iRight of idx) {
289+
while (stk.length) {
290+
const iLeft = stk.at(-1)!;
291+
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
292+
if (!havePair) break;
293+
294+
if (healths[iLeft] === healths[iRight]) {
295+
healths[iLeft] = healths[iRight] = iRight = -1;
296+
stk.pop();
297+
break;
298+
}
299+
300+
if (healths[iLeft] < healths[iRight]) {
301+
healths[iLeft] = -1;
302+
healths[iRight]--;
303+
stk.pop();
304+
} else {
305+
healths[iRight] = iRight = -1;
306+
healths[iLeft]--;
307+
break;
308+
}
309+
}
310+
311+
if (iRight !== -1) stk.push(iRight);
312+
}
313+
314+
return healths.filter(i => ~i);
315+
}
316+
```
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+
275363
<!-- tabs:end -->
276364

277365
<!-- 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+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function survivedRobotsHealths(
2+
positions: number[],
3+
healths: number[],
4+
directions: string,
5+
): number[] {
6+
const idx = Array.from({ length: positions.length }, (_, i) => i);
7+
const stk: number[] = [];
8+
9+
idx.sort((a, b) => positions[a] - positions[b]);
10+
11+
for (let iRight of idx) {
12+
while (stk.length) {
13+
const iLeft = stk.at(-1)!;
14+
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
15+
if (!havePair) break;
16+
17+
if (healths[iLeft] === healths[iRight]) {
18+
healths[iLeft] = healths[iRight] = iRight = -1;
19+
stk.pop();
20+
break;
21+
}
22+
23+
if (healths[iLeft] < healths[iRight]) {
24+
healths[iLeft] = -1;
25+
healths[iRight]--;
26+
stk.pop();
27+
} else {
28+
healths[iRight] = iRight = -1;
29+
healths[iLeft]--;
30+
break;
31+
}
32+
}
33+
34+
if (iRight !== -1) stk.push(iRight);
35+
}
36+
37+
return healths.filter(i => ~i);
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /