From dab7068e5029df0d1165cb6bfabedbf0e4c05b6e Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 23 Jul 2024 18:56:25 +0300 Subject: [PATCH 1/2] feat: add ts solution to lc problem: No.2751 --- .../2700-2799/2751.Robot Collisions/README.md | 43 +++++++++++++++++++ .../2751.Robot Collisions/README_EN.md | 43 +++++++++++++++++++ .../2751.Robot Collisions/Solution.ts | 38 ++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.ts diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index 276c770bb4572..b37b69c79190a 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -272,6 +272,49 @@ 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); +} +``` + diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index d3516d9da0290..80d83ef39c556 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -272,6 +272,49 @@ 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); +} +``` + diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.ts b/solution/2700-2799/2751.Robot Collisions/Solution.ts new file mode 100644 index 0000000000000..b765420e1d7ae --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.ts @@ -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); +} From cd8ccf5c381d759e3d5a52b31bf026982309951a Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 23 Jul 2024 18:59:16 +0300 Subject: [PATCH 2/2] feat: add js solution to lc problem: No.2751 --- .../2700-2799/2751.Robot Collisions/README.md | 45 +++++++++++++++++++ .../2751.Robot Collisions/README_EN.md | 45 +++++++++++++++++++ .../2751.Robot Collisions/Solution.js | 40 +++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.js diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index b37b69c79190a..3cb85e35ba416 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -315,6 +315,51 @@ function survivedRobotsHealths( } ``` +#### 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); +}; +``` + diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index 80d83ef39c556..42b59486b2825 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -315,6 +315,51 @@ function survivedRobotsHealths( } ``` +#### 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); +}; +``` + diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.js b/solution/2700-2799/2751.Robot Collisions/Solution.js new file mode 100644 index 0000000000000..3ad13fb4a37ae --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.js @@ -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); +};