https://programmers.co.kr/learn/courses/30/lessons/42626
코딩테스트 연습 - 더 맵게
매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
#include <queue>
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]);
}
int cnt = 0;
while (1) {
if (pq.size() == 1 && pq.top() < K)
return -1;
if (pq.top() > K)
break;
int pq1 = pq.top();
pq.pop();
int pq2 = pq.top();
pq.pop();
int tmp = pq1 + (pq2 * 2);
pq.push(tmp);
cnt++;
}
answer = cnt;
return answer;
}
int main() {
cout << solution({ 1, 2, 3, 9, 10, 12 }, 7);// 2
return 0;
}
'APS > 프로그래머스' 카테고리의 다른 글
[2017 팁스타운] 짝지어 제거하기 C++ (0) | 2022.01.09 |
---|---|
[힙(Heap)] 이중우선순위큐 C++ (0) | 2022.01.09 |
[2020 KAKAO BLIND RECRUITMENT] 문자열 압축 C++ (0) | 2022.01.02 |
[2017 카카오코드 본선] 단체사진 찍기 C++ (0) | 2021.12.31 |
[Summer/Winter Coding(~2018)] 영어 끝말잇기 C++ (0) | 2021.12.30 |