TIL (Today I Learned)

99클럽 코테 스터디 38일차 TIL + 오늘의 학습 키워드

남 희 2024. 8. 29. 10:56

☑️ 문제:  디펜스 게임

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

 

프로그래머스

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

programmers.co.kr

 

☑️ Code

import java.util.PriorityQueue;
import java.util.Collections;

class Solution {

    public int solution(int n, int k, int[] enemy) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

        int answer = 0;
        for (int e : enemy) {
            pq.offer(e);
            answer++;
            n -= e;
            if (n < 0) {
                if(k==0){
                    return answer - 1;
                }
                n = n + pq.poll();
                k--;
            }
        }

        return enemy.length;
    }
}