https://school.programmers.co.kr/learn/courses/30/lessons/42626
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int solution(vector<int> scoville, int K) {
//24.10.15
int answer = 0;
priority_queue<int, vector<int>, greater<int>> pq;
for_each(scoville.begin(), scoville.end(), [&](auto p) { pq.emplace(p); });
while (!pq.empty() && pq.size() >= 2)
{
if (pq.top() >= K) break;
auto a = pq.top(); pq.pop();
auto b = pq.top(); pq.pop();
auto newK = a + b * 2;
pq.emplace(newK);
++answer;
}
return pq.top() < K ? -1 : answer;
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] (정렬) Lv 2. H-Index (0) | 2024.10.17 |
---|---|
[Programmers] (Summer/Winter Coding(~2018)) Lv 2. 점프와 순간 이동 (0) | 2024.10.17 |
[Programmers] (2017 팁스타운) Lv 2. 짝지어 제거하기 (1) | 2024.10.15 |
[Programmers] (Summer/Winter Coding(~2018)) Lv 2. 배달 (0) | 2023.12.04 |
[Programmers] Lv 2. 줄 서는 방법 (0) | 2023.11.16 |
댓글