문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/12987
제한 사항
입출력 예
풀이
import java.util.PriorityQueue;
class Solution {
public int solution(int[] A, int[] B) {
int answer = 0;
PriorityQueue<Integer> aQueue = new PriorityQueue<>((i, j) -> j - i);
PriorityQueue<Integer> bQueue = new PriorityQueue<>((i, j) -> j - i);
for (int a : A) aQueue.add(a);
for (int b : B) bQueue.add(b);
while (aQueue.size() > 0) {
int a = aQueue.poll();
int b = bQueue.peek();
if (b > a) {
answer++;
bQueue.poll();
}
}
return answer;
}
}
후기
가장 높은 수를 가진 사람대로 세워놓고 이길 수 있는 것만 이기자 하는 것이 최선의 방법이다.
풀고 보니까 굳이 PriorityQueue를 사용하지 않고 정렬만 해서 풀어도 됐을 것 같다.
'코딩테스트 (프로그래머스) > Java' 카테고리의 다른 글
[프로그래머스][Java][Lv. 3] 최고의 집합 (0) | 2024.09.11 |
---|---|
[프로그래머스][Java][Lv. 3] 단속카메라 (0) | 2024.09.10 |
[프로그래머스][Java][Lv. 3] 등굣길 (0) | 2024.09.03 |
[프로그래머스][Java][Lv. 3] 단어 변환 (0) | 2024.09.02 |
[프로그래머스][Java][Lv. 3] 야근 지수 (0) | 2024.09.01 |