알고리즘 · 코딩
[프로그래머스] 더 맵게
G A
2020. 5. 12. 17:16
문제 링크
https://programmers.co.kr/learn/courses/30/lessons/42626
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
C++ 풀이
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(vector<int> scoville, int K) {
int answer = 0;
int m, n;
priority_queue<int, vector<int>, greater<int> > pq;
for (int i = 0; i < scoville.size(); i++)
pq.push(scoville[i]);
while ((pq.top() < K) && (pq.size() > 1)) {
//가장 덜 매운 두 음식을 빼서 합치기
m = pq.top();
pq.pop();
n = pq.top();
pq.pop();
pq.push(m + (2 * n));
answer++;
}
if (pq.top() < K)
return -1;
return answer;
}
우선순위 큐(priority queue)를 활용하여 풀었다.
반응형