diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index 276c770bb4572..3cb85e35ba416 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -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); +}; +``` + diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index d3516d9da0290..42b59486b2825 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -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); +}; +``` + 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); +}; 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); +}