Skip to content

Commit 917f145

Browse files
Create last_stone_weight.cpp
1 parent 8c3b1eb commit 917f145

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

last_stone_weight.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int lastStoneWeight(vector<int>& stones) {
4+
priority_queue<int> Q;
5+
for(auto stone : stones) {
6+
Q.emplace(stone);
7+
}
8+
9+
while(!Q.empty()) {
10+
if(Q.size() == 1) return Q.top();
11+
int x = Q.top(); Q.pop();
12+
int y = Q.top(); Q.pop();
13+
if(x != y)
14+
Q.push(x - y);
15+
}
16+
return 0;
17+
}
18+
};

0 commit comments

Comments
 (0)