[level2] 프로그래머스 - 다리를 지나는 트럭(JAVA)

[level2] 프로그래머스 - 다리를 지나는 트럭(JAVA)

728x90

- 우선순위 큐를 사용하면, 우선순위대로 정렬이 된다.

ex) [2, 1, 3, 2] -> [3, 2, 2, 1]

- queue의 맨 앞에 있는 숫자를 for문을 돌면서 찾아 지운다.

이때마다 answer을 증가시킨다.

- 지울 차례의 숫자가 location번째 숫자면, 바로 answer를 리턴한다.

import java.util.*; class Solution { public int solution(int[] priorities, int location) { int answer = 1; PriorityQueue queue = new PriorityQueue(Comparator.reverseOrder()); for(int n : priorities) queue.offer(n); while(!queue.isEmpty()) { for(int i = 0; i < priorities.length; i++) { if(queue.peek() == priorities[i]) { if(location == i) return answer; answer++; queue.poll(); } } } return answer; } }

728x90

from http://jisunshine.tistory.com/160 by ccl(A) rewrite - 2021-09-14 19:26:58