Skip to content

Commit dab7068

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

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,49 @@ 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+
275318
<!-- tabs:end -->
276319

277320
<!-- solution:end -->

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,49 @@ 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+
275318
<!-- tabs:end -->
276319

277320
<!-- solution:end -->
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)