-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1046.cpp
More file actions
32 lines (28 loc) · 669 Bytes
/
1046.cpp
File metadata and controls
32 lines (28 loc) · 669 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Problem : 1046. Last Stone Weight
// Link : https://leetcode.com/problems/last-stone-weight/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int lastStoneWeight(vector<int>& stones)
{
priority_queue<int> pq(stones.begin(),stones.end());
while(pq.size()>1)
{
int y=pq.top();
pq.pop();
int x=pq.top();
pq.pop();
if(x!=y) pq.push(y-x);
}
return pq.empty()? 0 : pq.top();
}
};
int main() {
Solution ob;
vector<int> arr{2,7,4,1,8,1};
cout << ob.lastStoneWeight(arr);
return 0;
}