Created
May 6, 2025 04:31
-
-
Save joshuabenson/205794dc8c0be099e57a68342db0fc87 to your computer and use it in GitHub Desktop.
Last Stone Weight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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