A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1046. Last Stone Weight, the solution in this repository is mainly a heap / priority queue solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: heap / priority queue, greedy.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are lastStoneWeight.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The queue gives the solution a level-by-level or frontier-style traversal.
- The heap keeps the best candidate available without sorting the whole world every time.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 60.28% of C++ online submissions for Last Stone Weight.
02//Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Last Stone Weight.
03
04class Solution {
05public:
06 int lastStoneWeight(vector<int>& stones) {
07 while(stones.size() > 1){
08 sort(stones.begin(), stones.end(), greater<int>());
09 int x = stones[0], y = stones[1];
10
11 stones.erase(stones.begin()+1);
12 stones.erase(stones.begin()+0);
13
14 if(x != y){
15 stones.push_back(abs(x-y));
16 }
17
18 }
19 return stones.size() > 0 ? stones[0] : 0;
20 }
21};
22
23//https://leetcode.com/problems/last-stone-weight/discuss/294956/JavaC%2B%2BPython-Priority-Queue
24//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Last Stone Weight.
25//Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Last Stone Weight.
26class Solution {
27public:
28 int lastStoneWeight(vector<int>& stones) {
29 std::priority_queue<int> pq(stones.begin(), stones.end());
30 while(pq.size() >= 2){
31 //y is always >= x
32 int y = pq.top(); pq.pop();
33 int x = pq.top(); pq.pop();
34
35 if(x != y) pq.push(y-x);
36 }
37
38 //note its pq.top() not pq[0]!
39 return pq.size() > 0 ? pq.top() : 0;
40 }
41};
Cost