Skip to content

Commit 4fc9041

Browse files
authored
Create last-stone-weight.cpp
1 parent 8b74a10 commit 4fc9041

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/last-stone-weight.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int lastStoneWeight(vector<int>& stones) {
7+
priority_queue<int> max_heap;
8+
for (const auto& x : stones) {
9+
max_heap.emplace(x);
10+
}
11+
for (int i = 0; i + 1 < stones.size(); ++i) {
12+
auto x = max_heap.top(); max_heap.pop();
13+
auto y = max_heap.top(); max_heap.pop();
14+
max_heap.emplace(abs(x - y));
15+
}
16+
return max_heap.top();
17+
}
18+
};

0 commit comments

Comments
 (0)