본문 바로가기
Coding Test/Programmers

[Programmers] (힙(Heap)) Lv 2. 더 맵게

by song.ift 2024. 10. 15.

https://school.programmers.co.kr/learn/courses/30/lessons/42626

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#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;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/c03660771a87176cc16574b1c8dc193ff7b9bd8a

 

[level 2] Title: 더 맵게, Time: 268.70 ms, Memory: 41.3 MB -BaekjoonHub · developeSHG/Algorithm-Baekjoon_Programmers@c036607

developeSHG committed Oct 15, 2024

github.com

 

 

댓글