Smallest Residue From Freely Chosen Stone Smashes

Quick Overview

Smallest Residue From Freely Chosen Stone Smashes. You may choose any two remaining stones to smash.

Smallest Residue From Freely Chosen Stone Smashes

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem You may choose any two remaining stones to smash. Equal weights disappear; weights x<y become a single stone of weight y-x. Continue until at most one remains. Return the smallest final weight achievable, counting an empty result as 0. ```cpp class Solution { public: int lastStoneWeightII(vector<int>& stones); }; ``` ### Examples **Example 1:** ```text Input: stones = [1,2,3,6] Output: 0 Explanation: Smash6 with3 to get3, then3 with2 to get1, then the two1s vanish. ``` **Example 2:** ```text Input: stones = [2,2,5] Output: 1 Explanation:5-2=3 and3-2=1. The total is odd, so residue0 is impossible. ``` ### Constraints - The input contains 1 to 30 stones. - Each weight is an integer between 1 and 100, inclusive. ### Contract considerations Any pair can be chosen, but the returned optimum weight is unique. ```hint Read the boundary condition Is choosing the heaviest pair required here, or may each turn choose any remaining pair? ``` ### Boundary reminders Any pair can be chosen, but the returned optimum weight is unique.

Overview: Smallest Residue From Freely Chosen Stone Smashes. You may choose any two remaining stones to smash.

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

Problem

You may choose any two remaining stones to smash. Equal weights disappear; weights x<y become a single stone of weight y-x. Continue until at most one remains. Return the smallest final weight achievable, counting an empty result as 0.

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

Examples

Example 1:

Input: stones = [1,2,3,6]
Output: 0
Explanation: Smash6 with3 to get3, then3 with2 to get1, then the two1s vanish.

Example 2:

Input: stones = [2,2,5]
Output: 1
Explanation:5-2=3 and3-2=1. The total is odd, so residue0 is impossible.

Constraints

  • The input contains 1 to 30 stones.
  • Each weight is an integer between 1 and 100, inclusive.

Contract considerations

Any pair can be chosen, but the returned optimum weight is unique.

Boundary reminders

Any pair can be chosen, but the returned optimum weight is unique.