From c7f33510489336c401b7ad58fc7d80ad2beedfc7 Mon Sep 17 00:00:00 2001 From: rl544 <111494264+rl544@users.noreply.github.com> Date: Sat, 31 Dec 2022 02:57:40 +0900 Subject: [PATCH] =?UTF-8?q?uploaded=20at=202022.=2012.=2031.=20=EC=98=A4?= =?UTF-8?q?=EC=A0=84=202:57:40?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/디펜스 게임/solution.java | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 programmers/디펜스 게임/solution.java diff --git a/programmers/디펜스 게임/solution.java b/programmers/디펜스 게임/solution.java new file mode 100644 index 0000000..12b3955 --- /dev/null +++ b/programmers/디펜스 게임/solution.java @@ -0,0 +1,29 @@ +// [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/142085 + +import java.util.PriorityQueue; +class Solution { + public int solution(int n, int k, int[] enemy) { + int answer = 0, min = enemy[0]; // k = defence + PriorityQueue pq = new PriorityQueue<>(); + for(int a = 0; a < Math.min(k,enemy.length); a++) { + pq.add(enemy[a]); + answer++; + } + for(int a = k; a < enemy.length; a++){ + if(n==0) break; + if(enemy[a]>pq.peek()){ + if(n >= pq.peek()){ + n -= pq.poll(); + pq.add(enemy[a]); + answer++; + continue; + } else break; + } + if(n >= enemy[a]){ + n -= enemy[a]; + answer++; + } else break; + } + return answer; + } +} \ No newline at end of file