Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Last Stone Weight

Easy
heapsimulation

You have stones with positive integer weights. Each turn, smash the two heaviest together: equal weights → both destroyed; otherwise the heavier survives with the difference. Return the weight of the last stone, or 0 if none remain.

Input: one line of space-separated integers. Output: the final weight (or 0).

Example 1
Input:
2 7 4 1 8 1
Output:
1

8&7→1, 4&2→2, 2&1→1, 1&1→0 leaves 1.

Example 2
Input:
1
Output:
1

Constraints

  • 1 ≤ stones.length ≤ 30
  • 1 ≤ stones[i] ≤ 1000
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.