-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ42.cpp
More file actions
33 lines (26 loc) · 662 Bytes
/
Copy pathQ42.cpp
File metadata and controls
33 lines (26 loc) · 662 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
33
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <iostream>
using namespace std;
int solution(vector<int> scoville, int K) {
int answer = 0;
priority_queue<int, vector<int>, greater<int>> pq(scoville.begin(), scoville.end());
while (true) {
int a = pq.top();
pq.pop();
if (a >= K) {
return answer;
}
if (pq.empty()) {
return -1;
}
int b = pq.top();
pq.pop();
int result = a < b ? a + (b * 2) : (a * 2) + b;
answer++;
pq.push(result);
}
return -1;
}