https://school.programmers.co.kr/learn/courses/30/lessons/142085
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.*;
class Solution {
public int solution(int n, int k, int[] enemy) {
int answer = 0;
int cnt = 0;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
int sum = 0;
for(int num : enemy){
answer++;
pq.offer(num);
sum += num;
if(sum > n && cnt < k){
int temp = pq.poll();
sum -= temp;
cnt++;
continue;
}
if(sum > n){
answer--;
break;
}
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 혼자 놀기의 달인 (JAVA) (0) | 2023.05.01 |
---|---|
프로그래머스 광물 캐기 (JAVA) (0) | 2023.04.30 |
프로그래머스 2의 영역(JAVA) (0) | 2023.04.28 |
프로그래머스 qr code (JAVA) (0) | 2023.04.27 |
프로그래머스 전국 대회 선별 고사 (JAVA) (0) | 2023.04.25 |