File tree 2 files changed +45
-3
lines changed
2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Problem: 1046
3
+ * Name: Last Stone Weight
4
+ * Difficulty: Easy
5
+ * Topic: Priority Queue
6
+ * Link: https://leetcode.com/problems/last-stone-weight/
7
+ */
8
+
9
+ #include < bits/stdc++.h>
10
+ using namespace std ;
11
+
12
+ // Naive Approach
13
+ // Time Complexity: O(n log n)
14
+ // Space Complexity: O(1)
15
+ int lastStoneWeightNaive (vector<int >& stones) {
16
+ while (stones.size () > 1 ){
17
+ sort (stones.begin (), stones.end ());
18
+ int largest = stones[stones.size ()-1 ];
19
+ int secondLargest = stones[stones.size ()-2 ];
20
+ stones.pop_back ();
21
+ if (largest == secondLargest){
22
+ stones.pop_back ();
23
+ }
24
+ else {
25
+ stones[stones.size ()-1 ] = largest-secondLargest;
26
+ }
27
+ }
28
+ return stones.empty () ? 0 : stones[0 ];
29
+ }
30
+
31
+ // Priority Queue
32
+ // Time Complexity: O(n)
33
+ // Space Complexity: O(n)
34
+ int lastStoneWeightPQ (vector<int >& stones) {
35
+ priority_queue<int > remainingStones (stones.begin (), stones.end ());
36
+ while (remainingStones.size () > 1 ){
37
+ int largest = remainingStones.top (); remainingStones.pop ();
38
+ int secondLargest = remainingStones.top (); remainingStones.pop ();
39
+ remainingStones.push (largest - secondLargest);
40
+ }
41
+ return remainingStones.top ();
42
+ }
Original file line number Diff line number Diff line change 16
16
17
17
### Problems Solved
18
18
19
- | Total | 42 |
19
+ | Total | 43 |
20
20
| :---:| :---:|
21
21
22
22
#### Search By Topic
36
36
| Intervals | 1 |
37
37
| Linked Lists | 5 |
38
38
| Math & Geometry | 2 |
39
- | Priority Queue | 1 |
39
+ | Priority Queue | 2 |
40
40
| Sliding Window | 1 |
41
41
| Stack | 2 |
42
42
| Tries | 0 |
46
46
47
47
| Difficulty | Number |
48
48
| :---| ---:|
49
- | Easy | 41 |
49
+ | Easy | 42 |
50
50
| Medium | 1 |
51
51
| Hard | 0 |
52
52
You can’t perform that action at this time.
0 commit comments