Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created September 21, 2019 02:01
Show Gist options
  • Save surinoel/2fb2504cae8aa731d53272cf18d2023b to your computer and use it in GitHub Desktop.
Save surinoel/2fb2504cae8aa731d53272cf18d2023b to your computer and use it in GitHub Desktop.
#include <queue>
#include <iostream>
#include <functional>
using namespace std;
int solution(vector<int> scoville, int K) {
int answer = 0;
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < scoville.size(); i++) {
pq.push(scoville[i]);
}
while (1) {
if (pq.size() <= 1) {
return -1;
}
if (pq.top() >= K) {
break;
}
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
int c = a + b * 2;
pq.push(c);
answer += 1;
}
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment