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

feat: add js solution to lc problem: No.2751 #3313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 2 commits into doocs:main from rain84:feature/lc-2751
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,94 @@ func survivedRobotsHealths(positions []int, healths []int, directions string) []
}
```

#### TypeScript

```ts
function survivedRobotsHealths(
positions: number[],
healths: number[],
directions: string,
): number[] {
const idx = Array.from({ length: positions.length }, (_, i) => i);
const stk: number[] = [];

idx.sort((a, b) => positions[a] - positions[b]);

for (let iRight of idx) {
while (stk.length) {
const iLeft = stk.at(-1)!;
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
if (!havePair) break;

if (healths[iLeft] === healths[iRight]) {
healths[iLeft] = healths[iRight] = iRight = -1;
stk.pop();
break;
}

if (healths[iLeft] < healths[iRight]) {
healths[iLeft] = -1;
healths[iRight]--;
stk.pop();
} else {
healths[iRight] = iRight = -1;
healths[iLeft]--;
break;
}
}

if (iRight !== -1) stk.push(iRight);
}

return healths.filter(i => ~i);
}
```

#### JavaScript

```js
/**
* @param {number[]} positions
* @param {number[]} healths
* @param {string} directions
* @return {number[]}
*/
var survivedRobotsHealths = function (positions, healths, directions) {
const idx = Array.from({ length: positions.length }, (_, i) => i);
const stk = [];

idx.sort((a, b) => positions[a] - positions[b]);

for (let iRight of idx) {
while (stk.length) {
const iLeft = stk.at(-1);
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
if (!havePair) break;

if (healths[iLeft] === healths[iRight]) {
healths[iLeft] = healths[iRight] = iRight = -1;
stk.pop();
break;
}

if (healths[iLeft] < healths[iRight]) {
healths[iLeft] = -1;
healths[iRight]--;
stk.pop();
} else {
healths[iRight] = iRight = -1;
healths[iLeft]--;
break;
}
}

if (iRight !== -1) stk.push(iRight);
}

return healths.filter(i => ~i);
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
88 changes: 88 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,94 @@ func survivedRobotsHealths(positions []int, healths []int, directions string) []
}
```

#### TypeScript

```ts
function survivedRobotsHealths(
positions: number[],
healths: number[],
directions: string,
): number[] {
const idx = Array.from({ length: positions.length }, (_, i) => i);
const stk: number[] = [];

idx.sort((a, b) => positions[a] - positions[b]);

for (let iRight of idx) {
while (stk.length) {
const iLeft = stk.at(-1)!;
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
if (!havePair) break;

if (healths[iLeft] === healths[iRight]) {
healths[iLeft] = healths[iRight] = iRight = -1;
stk.pop();
break;
}

if (healths[iLeft] < healths[iRight]) {
healths[iLeft] = -1;
healths[iRight]--;
stk.pop();
} else {
healths[iRight] = iRight = -1;
healths[iLeft]--;
break;
}
}

if (iRight !== -1) stk.push(iRight);
}

return healths.filter(i => ~i);
}
```

#### JavaScript

```js
/**
* @param {number[]} positions
* @param {number[]} healths
* @param {string} directions
* @return {number[]}
*/
var survivedRobotsHealths = function (positions, healths, directions) {
const idx = Array.from({ length: positions.length }, (_, i) => i);
const stk = [];

idx.sort((a, b) => positions[a] - positions[b]);

for (let iRight of idx) {
while (stk.length) {
const iLeft = stk.at(-1);
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
if (!havePair) break;

if (healths[iLeft] === healths[iRight]) {
healths[iLeft] = healths[iRight] = iRight = -1;
stk.pop();
break;
}

if (healths[iLeft] < healths[iRight]) {
healths[iLeft] = -1;
healths[iRight]--;
stk.pop();
} else {
healths[iRight] = iRight = -1;
healths[iLeft]--;
break;
}
}

if (iRight !== -1) stk.push(iRight);
}

return healths.filter(i => ~i);
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
40 changes: 40 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/Solution.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @param {number[]} positions
* @param {number[]} healths
* @param {string} directions
* @return {number[]}
*/
var survivedRobotsHealths = function (positions, healths, directions) {
const idx = Array.from({ length: positions.length }, (_, i) => i);
const stk = [];

idx.sort((a, b) => positions[a] - positions[b]);

for (let iRight of idx) {
while (stk.length) {
const iLeft = stk.at(-1);
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
if (!havePair) break;

if (healths[iLeft] === healths[iRight]) {
healths[iLeft] = healths[iRight] = iRight = -1;
stk.pop();
break;
}

if (healths[iLeft] < healths[iRight]) {
healths[iLeft] = -1;
healths[iRight]--;
stk.pop();
} else {
healths[iRight] = iRight = -1;
healths[iLeft]--;
break;
}
}

if (iRight !== -1) stk.push(iRight);
}

return healths.filter(i => ~i);
};
38 changes: 38 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/Solution.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function survivedRobotsHealths(
positions: number[],
healths: number[],
directions: string,
): number[] {
const idx = Array.from({ length: positions.length }, (_, i) => i);
const stk: number[] = [];

idx.sort((a, b) => positions[a] - positions[b]);

for (let iRight of idx) {
while (stk.length) {
const iLeft = stk.at(-1)!;
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
if (!havePair) break;

if (healths[iLeft] === healths[iRight]) {
healths[iLeft] = healths[iRight] = iRight = -1;
stk.pop();
break;
}

if (healths[iLeft] < healths[iRight]) {
healths[iLeft] = -1;
healths[iRight]--;
stk.pop();
} else {
healths[iRight] = iRight = -1;
healths[iLeft]--;
break;
}
}

if (iRight !== -1) stk.push(iRight);
}

return healths.filter(i => ~i);
}

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