728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42628?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.*;
class Solution {
public int[] solution(String[] operations) {
int[] answer = {};
PriorityQueue<Integer> pq = new PriorityQueue<>(); // 오름차순
PriorityQueue<Integer> reversePq = new PriorityQueue<>(Collections.reverseOrder()); // 내림차순
for(String n : operations) {
String[] arr = n.split(" ");
String check = arr[0];
int value = Integer.parseInt(arr[1]);
if(check.equals("I")) {
pq.add(value);
reversePq.add(value);
} else if (check.equals("D") && value == -1) {
if(!pq.isEmpty()) {
int a = pq.poll();
reversePq.remove(a);
}
} else if (check.equals("D") && value == 1) {
if(!reversePq.isEmpty()) {
int maxValue = reversePq.poll();
pq.remove(maxValue);
}
}
}
if(pq.isEmpty()) {
answer = new int[]{0, 0};
} else {
answer = new int[]{reversePq.poll(), pq.poll()};
}
return answer;
}
}
풀이
- 우선순위 큐 두개로 내림차순 오름차순을 만들어 각각 데이터를 저장한다
- 저장 후 문자열을 비교하여 최솟값과 최댓값을 빼준다
- 모든 것을 탐색하고 queue에 데이터가 남아있으면 최소 최댓값을 출력 비어 있으면 0 0 리턴
회고
- queue 두개로 queue와 stack을 둘 다 사용할 수 있냐고 묻는 문제 같았다
'Algorithm Study > Programmers (JAVA)' 카테고리의 다른 글
프로그래머스 Lv3_단어 변환_Java (0) | 2024.06.27 |
---|---|
프로그래머스 Lv3_야근 지수_Java (0) | 2024.06.26 |
프로그래머스 Lv2_올바른 괄호_Java (0) | 2024.06.25 |
프로그래머스 Lv3_네트워크_Java (0) | 2024.06.23 |
프로그래머스 Lv3_정수 삼각형_Java (0) | 2024.06.23 |