[Programmers] Lv2. λ λ§΅κ² | C++
2023. 6. 21. 16:23γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π¨π»νμ΄ κ³Όμ
λ¬Έμ μ νλ ν(Heap)μ΄λΌ λΆλ₯λμ΄ μμμ§λ§, μ§λ¬Έμ μ½μλ§μ μ΅μ ν(Min Heap)μ΄ μκ°λλ λ¬Έμ μμ΅λλ€. μ λ ₯ λ²μ μ«μκ° λ§€μ° ν¬λ―λ‘, μ€λ²νλ‘μ°λ§ μ‘°μ¬νλ©΄ μμ½κ² ν μ μμμ΅λλ€.
βοΈμμ€ μ½λ λ° κ²°κ³Ό
#include <string>
#include <vector>
#include <queue>
using namespace std;
using LL = long long;
int solution(vector<int> scoville, int K) {
int answer = 0;
priority_queue<LL, vector<LL>, greater<LL>> scovilles;
for (const auto element : scoville)
scovilles.emplace(element);
while (scovilles.size() >= 2) {
LL leastSpicyFood = scovilles.top();
scovilles.pop();
LL secondLeastSpicyFood = scovilles.top();
scovilles.pop();
if (leastSpicyFood >= K)
break;
LL newScovill = leastSpicyFood + secondLeastSpicyFood * 2;
answer++;
scovilles.emplace(newScovill);
}
if (scovilles.top() < K)
return -1;
return answer;
}
728x90
λ°μν
'Coding Test > Programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers] Lv2. μ€νμ±ν λ°© | C++ (0) | 2023.06.21 |
---|---|
[Programmers] Lv2. [3μ°¨] nμ§μ κ²μ | C++ (0) | 2023.06.21 |
[Programmers] Lv2. kμ§μμμ μμ κ°μ ꡬνκΈ° | C++ (0) | 2023.06.21 |
[Programmers] Lv2. νκ² λλ² | C++ (0) | 2023.06.20 |
[Programmers] Lv2. λ΄μ€ ν΄λ¬μ€ν°λ§ | C++ (0) | 2023.06.20 |