문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/42587
제한 사항
입출력 예
풀이
import java.util.Queue;
import java.util.LinkedList;
class Solution {
public int solution(int[] priorities, int location) {
Queue<Integer> result = new LinkedList<>();
int answer = 0;
int max = 9;
for (int i : priorities) result.add(i);
while(result.size() != 0) {
while (max != 0) {
if (!result.contains(max)) max--;
else break;
}
if (result.peek() == max) {
result.poll();
answer++;
if (location == 0) return answer;
else location--;
}
else {
result.add(result.poll());
if (location == 0) location = result.size() - 1;
else location--;
}
}
return answer;
}
}
후기
오랜만에 첫 시도에 바로 통과했다. 그런데도 1점... 아무래도 다른 사람들도 다 이 방법으로 풀어서 그런 것 같다. 카테고리도 그렇고 문제에서도 대놓고 큐를 쓰라고 해서 다른 방법은 딱히 안 떠올라서 그냥 큐를 써서 풀었다. location이 0일 때 max값이면 그대로 끝내고 아니라면 맨 끝 인덱스로 조절해 주는 처리만 잘하면 무난하게 풀 수 있다.
'코딩테스트 (프로그래머스) > Java' 카테고리의 다른 글
[프로그래머스][JAVA][Lv. 3] 이중우선순위큐 (0) | 2023.09.15 |
---|---|
[프로그래머스][JAVA][Lv. 2] 뉴스 클러스터링 (0) | 2023.09.14 |
[프로그래머스][JAVA][Lv. 0] 배열 조각하기 (0) | 2023.09.11 |
[프로그래머스][JAVA][Lv. 2] 기능 개발 (0) | 2023.09.08 |
[프로그래머스][JAVA][Lv. 2] 괄호 회전하기 (0) | 2023.09.07 |