📚 문제

이미지를 클릭하시면 문제 링크로 연결됩니다.


📝 문제 이해

  • 우선순위를 가지고 있는 배열에서 찾고싶은 인덱스를 언제 반환하는지 찾으면 되겠다.
  • 숫자가 큰 숫자부터 제거

💡 문제 풀이

  • 우선순위를 담은 배열을 우선순위 큐에 담아준다.
  • 큐의 요소가 없어질 때까지 루프
  • 배열의 우선순위와 큐의 우선순위가 같으면 해당 큐를 제거하고 카운트를 증가시킨다.
  • 인덱스 i 와 찾고싶은 인덱스인 location 이 같으면 증가시킨 카운트를 출력한다.

💻 소스 코드

import java.util.Collections;
import java.util.PriorityQueue;

// 5일차 (연결리스트) - 프로그래머스 프로세스 문제 (스택/큐)
public class day05Prog42587 {
    public static int solution(int[] priorities, int location) {
        int answer = 0;
        // 우선순위 큐 (내림차순)
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

        // priorities 의 원소들을 우선순위 큐에 넣어준다.
        for(int i : priorities) pq.add(i);

        while(!pq.isEmpty()) {  // 큐가 빌때까지 루프
            for (int i = 0; i < priorities.length; i++) {
                if(priorities[i] == pq.peek()){ // 배열의 우선순위와 큐의 우선순위가 같다면
                    pq.poll();                  // 해당 큐를 제거해주고
                    answer++;                   // 카운트를 증가시킨다.
                    if (i == location) {        // 이때, 찾는 location 이라면
                        return answer;          // 답을 반환한다.
                    }
                }
            }
        }
        return answer;
    }![](https://velog.velcdn.com/images/huthut_kwon/post/138566e6-a698-4626-b862-b68f29506dce/image.png)

}

0개의 댓글