[Programmers] 더맵게

Time Lapse :25min 25sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <queue>
using namespace std;

int solution(vector<int> scoville, int K) {
priority_queue<int,vector<int>,greater<int>> pq (scoville.begin(),scoville.end());
int answer = 0, worst;
bool flag = false;
while(!pq.empty()&&(worst = pq.top())<K){
pq.pop();
if(pq.empty()){
if(flag)
return answer + 1;
else
return -1;
}
if((pq.top()<<1) + worst<K) pq.push((pq.top()<<1) + worst);
else flag = true;
++answer;
pq.pop();
}
return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/hotter/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.