Code Room
CodingMediumcod-g030
Subject KnapsackLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You have a collection of stones with positive integer weights. In each turn you smash any two stones x and y (x <= y): they both vanish and a new stone of weight y-x appears (if y-x is 0 nothing remains). After repeatedly smashing, at most one stone is left. Return the smallest possible weight of that last stone (0 if none remains). There are at most 30 stones, each of weight at most 100.

Implement
last_stone_weight_ii(stones: list[int]) → int
Examples
in[[2,7,4,1,8,1]]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.