프로그래머스 문제풀이/LEVEL 2

[C++] 프로그래머스 문제풀이 LEVEL 2 디펜스 게임

코딩준우 2023. 7. 5. 13:08

 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

#include <queue>
#include <vector>

using namespace std;

int solution(int n, int k, vector<int> enemy)
{
    int answer = 0;

    int idx = 0;
    priority_queue<int> pq;
    while(idx < enemy.size())
    {
        if(n < enemy[idx] && k <= 0) break;
        n -= enemy[idx];
        pq.push(enemy[idx]);
        if(n < 0)
        {
            n += pq.top();
            pq.pop();
            --k
        }
        ++idx;
    }
    answer = idx;
    
    return answer;
}