프로그래머스(Programmers)
힙(Heap) > 더 맵게
cepiloth
2018. 9. 30. 19:09
반응형
https://programmers.co.kr/learn/courses/30/lessons/42626
1. 문제
2. 알고리즘
키워드 - 힙
3. 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <string> #include <vector> #include <algorithm> #include <functional> #include <queue> using namespace std; int solution(vector<int> scoville, int K) { int answer = 0; priority_queue<int, vector<int>, greater<int>> q; for (int i = 0; i < scoville.size(); i++) { q.push(scoville[i]); } while (true) { int a = q.top(); if (a >= K) { break; } if(q.size() == 1) { answer = -1; break; } q.pop(); int b = q.top(); q.pop(); int cand = a + (b * 2); q.push(cand); answer++; } return answer; } | cs |
반응형