본문
프린터 #스택/큐 #level2
프로그래밍/코딩테스트(java) 2021. 1. 9. 20:50
반응형
💡 입출력 예 - 부연설명
💡 실행
public class Main {
public static final String UTC_ID = "Etc/UTC";
public static void main(String[] args) {
int[] priorities = { 2, 1, 3, 2 };
int location = 2;
System.out.println(new Solution().solution(priorities, location));
}
}
💡 풀이
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
int l = location;
Queue<Integer> q = new LinkedList<Integer>();
for (int i : priorities) {
q.add(i);
}
int size = priorities.length-1;
Arrays.sort(priorities);
// priorities: [1, 2, 2, 3]
System.out.println("priorities: " + Arrays.toString(priorities));
while (!q.isEmpty()) {
Integer i = q.poll();
if (i == priorities[size - answer]) {
answer++;
l--;
if (l < 0)
break;
} else {
q.add(i);
l--;
if (l < 0)
l = q.size() - 1;
}
}
return answer;
}
}
반응형
댓글