Final Ingot Weight
easyA smith melts down scrap by always feeding the two heaviest ingots into the crucible together. If they weigh the same, both vanish into slag. If not, the heavier one survives, reduced by the lighter one's weight, and goes back on the pile.
Given ingots (a list of positive weights), repeat this until at most one ingot remains. Return its weight, or 0 if the pile is empty.
Example 1
in ingots = [2, 7, 4, 1, 8, 1]
out 1
8 vs 7 -> 1. Pile [2,4,1,1,1]. 4 vs 2 -> 2. Pile [2,1,1,1]. 2 vs 1 -> 1. Pile [1,1,1]. 1 vs 1 -> gone. Pile [1]. Answer 1.
Example 2
in ingots = [3, 3]
out 0
Equal weights annihilate; nothing survives.
Constraints
- · 1 <= len(ingots) <= 30
- · 1 <= ingots[i] <= 1000
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.