Skip to content

Instantly share code, notes, and snippets.

@joshuabenson
Created May 6, 2025 04:31
Show Gist options
  • Save joshuabenson/205794dc8c0be099e57a68342db0fc87 to your computer and use it in GitHub Desktop.
Save joshuabenson/205794dc8c0be099e57a68342db0fc87 to your computer and use it in GitHub Desktop.
Last Stone Weight
/**
https://neetcode.io/problems/last-stone-weight
**/
class Solution {
/**
* @param {number[]} stones
* @return {number}
*/
lastStoneWeight(stones) {
while (stones.length > 1) {
// Sort descending
stones.sort((a, b) => b - a);
const y = stones.shift();
const x = stones.shift();
if (y !== x) {
stones.push(y - x);
}
}
return stones[0] || 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment