Snowball Showdown
A winter fair's snowball machine holds a pile of snowballs of different sizes. Each round, it fires the two biggest straight at each other.
Equal sizes both shatter. Otherwise the smaller one shatters and knocks that much snow off the bigger one, which drops back into the pile with its new size.
Your task
Write lastSnowball(sizes). Run rounds until fewer than two snowballs are left. Return the size of the one that remains, or 0 if none remain.
In a round where the two biggest have sizes a ≥ b: if a = b both disappear, otherwise a snowball of size a - b goes back into the pile.
lastSnowball(sizes: int[]) → int
Examples
sizes = [4, 9, 2, 6]
1
First 9 and 6 collide and a snowball of size 3 goes back, so the pile is [4, 2, 3]. Then 4 and 3 collide and a 1 goes back, leaving [2, 1]. Finally 2 and 1 collide and a 1 goes back, so one snowball of size 1 is left.
sizes = [5, 5, 3, 3]
0
5 and 5 shatter each other. Then 3 and 3 shatter each other. Nothing is left, so the answer is 0.
sizes = [7]
7
With only one snowball there is no round at all. The answer is 7.
Constraints
- 1 ≤ sizes.length ≤ 100,000
- 1 ≤ sizes[i] ≤ 1,000,000
Start with brute force
Sort the pile, take the two biggest from the end, put the difference back into the pile, and sort again.
Each sort is O(n log n) and there can be n rounds: O(n² log n), far beyond a billion steps for 100,000 snowballs. Inserting the leftover into a sorted array still costs O(n) per round for shifting, so O(n²) overall.
Hints
0 of 4 shown- Hint 1
In the first example, after 9 and 6 collide, a 3 goes back. Where does it belong among the remaining sizes? Could the leftover be the biggest next round?
- Hint 2
Each round needs the two largest snowballs of a pile that keeps changing, because two leave and possibly one comes back. Re-sorting after every round redoes almost all of the work.
- Hint 3
Which data structure gives you the largest item quickly and accepts a new one quickly, without keeping everything sorted?
- Hint 4
Use a max-heap and push every size into it. While the heap holds at least two snowballs, pop
aand thenb, and pusha - bifa > b. When the loop ends, return the top of the heap if one snowball is left, and 0 otherwise. (Python'sheapqis a min-heap; store negated sizes.)
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
"Always take the biggest two" is a job for a max-heap: it gives you the largest item and accepts a new one in O(log n) each, without sorting the whole pile.
Trace [4, 9, 2, 6]:
- Heap holds 9, 6, 4, 2. Pop 9 and 6. 9 > 6, push 3. Heap: 4, 3, 2.
- Pop 4 and 3. Push 1. Heap: 2, 1.
- Pop 2 and 1. Push 1. Heap: 1.
- Only one left. Return 1.
Each round removes at least one snowball for good, so there are at most n - 1 rounds. JavaScript has no built-in heap, so the solution writes the same sift-up and sift-down from the heaps lesson.
function lastSnowball(sizes) {
const heap = []; // max-heap: heap[0] is the biggest
const push = (x) => {
heap.push(x);
let i = heap.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (heap[p] >= heap[i]) break;
[heap[p], heap[i]] = [heap[i], heap[p]];
i = p;
}
};
const pop = () => {
const top = heap[0];
const last = heap.pop();
if (heap.length > 0) {
heap[0] = last;
let i = 0;
while (true) {
const l = 2 * i + 1, r = l + 1;
let big = i;
if (l < heap.length && heap[l] > heap[big]) big = l;
if (r < heap.length && heap[r] > heap[big]) big = r;
if (big === i) break;
[heap[big], heap[i]] = [heap[i], heap[big]];
i = big;
}
}
return top;
};
for (const s of sizes) push(s);
while (heap.length >= 2) {
const a = pop(); // biggest
const b = pop(); // second biggest
if (a > b) push(a - b);
}
return heap.length ? heap[0] : 0;
}import heapq
def last_snowball(sizes):
# heapq is a min-heap, so store negated sizes
heap = [-s for s in sizes]
heapq.heapify(heap)
while len(heap) >= 2:
a = -heapq.heappop(heap) # biggest
b = -heapq.heappop(heap) # second biggest
if a > b:
heapq.heappush(heap, -(a - b))
return -heap[0] if heap else 0Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.