https://school.programmers.co.kr/learn/courses/30/lessons/42587
풀이
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int[] arr = priorities.clone();
arr[location] += 10;
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i < arr.length; i++){
q.add(arr[i]);
}
Arrays.sort(priorities);
int answer = 0;
int idx = priorities.length -1;
int prior = priorities[idx];
while(!q.isEmpty()){
int temp = q.poll();
if(temp > 10 && temp - 10 == prior){
return answer + 1;
}else if(temp == prior){
answer++;
idx--;
prior = priorities[idx];
}else{
q.add(temp);
}
}
return answer;
}
}
location의 위치에 대한 해결이 중요한 문제였다.
location의 경우 특별한 마킹을 해줘야 함이 분명한데도 그것을 처리하기 위해 다른 자료구조를 쓰는 것은 너무 큰 손해같았다.
따라서 +10을 해줘서(우선순위는 1~9까지이므로 확실히 구분됨) 구분을 하고, 이후에 큐에서 꺼낸 값이 10보다 크다면을 따로 처리해줬다.
'문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 더 맵게(JAVA) (2) | 2024.04.26 |
---|---|
[프로그래머스] 다리를 지나는 트럭(JAVA) (0) | 2024.04.25 |
[프로그래머스] 올바른 괄호(JAVA) (0) | 2024.04.24 |
[프로그래머스] 기능개발(JAVA) (0) | 2024.04.24 |
[프로그래머스] 같은 숫자는 싫어(JAVA) (0) | 2024.04.24 |