In a small browser game, blocks with weights crash into each other. Each turn, the two heaviest blocks collide. If they weigh the same, both are destroyed. If not, the lighter one is destroyed and the heavier one loses that much weight and stays in the game.
Write lastBlock(weights) that plays until at most one block is left and returns its weight, or 0 if no block is left.
Input: weights = [3,9,5,1,10,1] Output: 1
10 and 9 leave 1; 5 and 3 leave 2; 2 and 1 leave 1; two 1s destroy each other, and one 1 is left.
Input: weights = [5,5] Output: 0
Equal weights destroy each other.
1 <= weights.length <= 10001 <= weights[i] <= 1000Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #378 Last Block Standing. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.