본문 바로가기
Coding Test/Programmers

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

by song.ift 2023. 7. 4.

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

 

프로그래머스

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

programmers.co.kr

 

#include <string>
#include <vector>
#include <queue>
using namespace std;

int solution(vector<int> scoville, int K) {
    int answer = 0;
    priority_queue<int, vector<int>, greater<int>> q;

    for (const auto& e : scoville) q.push(e);

    int first, second, result;
    while (true) {
        if (q.top() >= K) break;
        else if (q.size() <= 1) return -1;
        else {
            first = q.top(); q.pop();
            second = q.top(); q.pop();
            result = first + (second * 2);

            q.push(result), answer++;
        }
    }
    return answer;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/blob/42df80fdcc874957ccd882653c4419c271a7c78e/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/42626.%E2%80%85%EB%8D%94%E2%80%85%EB%A7%B5%EA%B2%8C/%EB%8D%94%E2%80%85%EB%A7%B5%EA%B2%8C.cpp

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글