LeetCode Problem Workspace
Last Stone Weight
In the 'Last Stone Weight' problem, we smash the two heaviest stones until one remains, using heaps or sorting.
2
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array plus Heap (Priority Queue)
Answer-first summary
In the 'Last Stone Weight' problem, we smash the two heaviest stones until one remains, using heaps or sorting.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array plus Heap (Priority Queue)
To solve 'Last Stone Weight', you simulate the process of smashing stones by repeatedly selecting the two heaviest ones. The game continues until one stone, or none, remains. Using heaps (priority queues) or sorting provides an efficient approach to solving this problem.
Problem Statement
You are given an array stones[] where each element represents the weight of a stone. On each turn, you must select the two heaviest stones, smash them together, and replace them with their absolute difference. The game ends when at most one stone remains.
For example, with stones = [2,7,4,1,8,1], you simulate smashing stones: the heaviest two, 8 and 7, are smashed to yield 1. This continues until only one stone remains. The task is to find the final stone weight, or return 0 if no stones remain.
Examples
Example 1
Input: stones = [2,7,4,1,8,1]
Output: 1
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Example 2
Input: stones = [1]
Output: 1
Example details omitted.
Constraints
- 1 <= stones.length <= 30
- 1 <= stones[i] <= 1000
Solution Approach
Use a Max-Heap
Convert the list of stones into a max-heap. This allows efficient retrieval of the two largest stones at each step. By repeatedly extracting and updating the heap, we simulate the stone-smashing process until only one stone remains.
Sort the Stones
Alternatively, you can sort the stones in descending order and simulate the process by picking the first two elements, performing the smash, and inserting the result back into the sorted array. This approach is less efficient than using a heap but still works within the problem's constraints.
Handle Edge Cases
Consider edge cases where the number of stones is very small (like one stone) or when all stones are of the same weight. These cases are simpler but need special handling in the solution to avoid unnecessary operations.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Both heap and sorting approaches involve repeated operations to find and modify the two largest stones. The heap approach typically has O(log n) complexity for insertion and extraction, while the sorting method involves O(n log n) complexity per turn. Thus, the time complexity depends on the chosen method and number of stones.
What Interviewers Usually Probe
- Candidate uses efficient heap operations to solve the problem.
- Candidate explains the trade-off between heaps and sorting for this problem.
- Candidate demonstrates how to handle edge cases such as one stone or identical stones.
Common Pitfalls or Variants
Common pitfalls
- Not using a heap efficiently, leading to higher time complexity than necessary.
- Over-complicating the solution with unnecessary steps when the problem can be solved with basic sorting or heaps.
- Failing to handle the case where only one stone remains.
Follow-up variants
- Extend the problem to handle different game variations where the number of stones can be modified after each turn.
- Instead of smashing stones, consider keeping track of the two smallest stones.
- Adjust the problem to allow multiple rounds of stone smashing, where new stones are added to the array after each turn.
FAQ
What is the best approach for solving the Last Stone Weight problem?
Using a max-heap provides the most efficient solution for the Last Stone Weight problem by allowing quick retrieval and modification of the two largest stones.
Can I solve the problem with sorting?
Yes, sorting works as an alternative approach, though it's less efficient than using a heap. Sorting allows easy access to the two largest stones but involves higher time complexity.
How do I handle edge cases in this problem?
Edge cases like a single stone or stones with identical weights can be handled by checking the length of the array or ensuring no unnecessary operations are performed when only one stone remains.
How does GhostInterview help with Last Stone Weight?
GhostInterview provides step-by-step assistance in understanding heap and sorting methods, helping you implement a clear and efficient solution.
What is the time complexity of the heap solution?
The heap solution has a time complexity of O(n log n), where n is the number of stones, due to repeated heap insertions and extractions.
Solution
Solution 1
#### Python3
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
h = [-x for x in stones]
heapify(h)
while len(h) > 1:
y, x = -heappop(h), -heappop(h)
if x != y:
heappush(h, x - y)
return 0 if not h else -h[0]Continue Practicing
Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array plus Heap (Priority Queue)
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward