Fewest Stones Left After Merging Equal-Level Pairs
Company: Elevenlabs
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Each stone has an integer level, and two stones of the same level can be merged into one stone of the next level, with merged stones able to merge again. Return the fewest stones that can remain after any sequence of merges, a problem that tests counting by level, cascading merges, and efficient handling of large inputs.
Read the full Elevenlabs Software Engineer interview experience this question came from
Constraints
- 1 <= len(stones) <= 10^5
- 1 <= stones[i] <= 9999
- Merged stones may reach level 10000 or higher; there is no upper limit on the level of a merged stone
- The answer is always between 1 and len(stones)
Examples
Input: ([1, 2, 1],)
Expected Output: 1
Explanation: Source Example 1: the two level-1 stones make a level-2 stone that re-merges with the existing level-2 stone into one level-3 stone.
Input: ([3, 3, 3],)
Expected Output: 2
Explanation: Source Example 2: an odd count at one level leaves one level-3 stone and one level-4 stone.
Hints
- Merges can happen in any order and any two stones of equal level can merge, so think about which information about the input actually matters.
- A stone created by a merge can merge again, so a single merge at a low level can trigger further merges higher up.
- Ask what stays the same no matter which merges you perform; it limits how few stones can remain.