자기개발/코딩테스트

C++ (코딩테스트) - 디펜스 게임(프로그래머스)

pi92 2022. 12. 28. 16:06

문제

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

 

프로그래머스

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

programmers.co.kr

 

내가 쓴 풀이

#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(int n, int k, vector<int> enemy) {
	priority_queue<int> pq;
	pq.empty();

	for (int i = 0; i < enemy.size(); i++)
	{
		pq.push(enemy[i]);
		n -= enemy[i];
		if (n < 0)
		{
			if (k-- <= 0) { return i; }
			n += pq.top();
			pq.pop();
		}
	}
	return enemy.size();
}

 

 

참고자료

(우선순위큐)

https://jungeu1509.github.io/algorithm/use-priorityqueue/