Final Weight Under Heaviest-Pair Smashing

Quick Overview

Final Weight Under Heaviest-Pair Smashing. Repeatedly take the two heaviest remaining stones. Equal weights both disappear; unequal weights x<y are replaced by one stone of weight y-x.

Final Weight Under Heaviest-Pair Smashing

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Repeatedly take the two heaviest remaining stones. Equal weights both disappear; unequal weights x<y are replaced by one stone of weight y-x. Return the last weight, or 0 when no stone remains. ```cpp class Solution { public: int lastStoneWeight(vector<int>& stones); }; ``` ### Examples **Example 1:** ```text Input: stones = [6,3,2] Output: 1 Explanation:6 and3 become3;3 and2 then become1. ``` **Example 2:** ```text Input: stones = [4,4,2,2] Output: 0 Explanation: The two4s disappear, then the two2s disappear. ``` ### Constraints - There are at least 1 and at most 30 stones. - Each weight is an integer in `[1,1000]`. ### Contract considerations The heaviest-pair choice is mandatory, rather than a decision to optimize. ```hint Read the boundary condition Does each turn use the original heaviest pair or the stones that remain after the preceding smash? ``` ### Boundary reminders The heaviest-pair choice is mandatory, rather than a decision to optimize.

Overview: Final Weight Under Heaviest-Pair Smashing. Repeatedly take the two heaviest remaining stones. Equal weights both disappear; unequal weights x<y are replaced by one stone of weight y-x.

Apple logo
Apple
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Repeatedly take the two heaviest remaining stones. Equal weights both disappear; unequal weights x<y are replaced by one stone of weight y-x. Return the last weight, or 0 when no stone remains.

class Solution {
public:
    int lastStoneWeight(vector<int>& stones);
};

Examples

Example 1:

Input: stones = [6,3,2]
Output: 1
Explanation:6 and3 become3;3 and2 then become1.

Example 2:

Input: stones = [4,4,2,2]
Output: 0
Explanation: The two4s disappear, then the two2s disappear.

Constraints

  • There are at least 1 and at most 30 stones.
  • Each weight is an integer in [1,1000] .

Contract considerations

The heaviest-pair choice is mandatory, rather than a decision to optimize.

Boundary reminders

The heaviest-pair choice is mandatory, rather than a decision to optimize.