[Programmers][JAVA] 프린터
📒 문제

📒 제한사항

👣 코드
import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
    public int solution(int[] priorities, int location) {
         int answer = 0;
			
         // 우선순위 큐, 내림차순    
         PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
    
		
        for (int i = 0; i < priorities.length; i++) {
            pq.offer(priorities[i]);
        }
        
        // queue가 비어있지 않는다면 
        while (!pq.isEmpty()) {
            for (int i = 0; i < priorities.length; i++) {
            	// pq는 내림차순 즉, 우선순위가 높은 순으로 정렬되어있음. 
                if (pq.peek() == priorities[i]) {
                    pq.poll();
                    answer++;
                    
                    if (i == location) {
                        pq.clear();
                        break;
                    }
                }
            }
        }
        return answer;
    }
}
💡 정리하기
👉 처음에 priority queue가 아닌 queue를 사용해서 하려니 계속 바뀌는 location을 찾는 부분에 막혀서 풀지 못했는데 priority queue를 사용하여 애초에 내림차순으로 정렬해서 location의 수만큼 거를 수 있었다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Author And Source
                            
                            이 문제에 관하여([Programmers][JAVA] 프린터), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://velog.io/@hye_b/ProgrammersJAVA-프린터
                            
                            
                            
                                저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
    public int solution(int[] priorities, int location) {
         int answer = 0;
			
         // 우선순위 큐, 내림차순    
         PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
    
		
        for (int i = 0; i < priorities.length; i++) {
            pq.offer(priorities[i]);
        }
        
        // queue가 비어있지 않는다면 
        while (!pq.isEmpty()) {
            for (int i = 0; i < priorities.length; i++) {
            	// pq는 내림차순 즉, 우선순위가 높은 순으로 정렬되어있음. 
                if (pq.peek() == priorities[i]) {
                    pq.poll();
                    answer++;
                    
                    if (i == location) {
                        pq.clear();
                        break;
                    }
                }
            }
        }
        return answer;
    }
}Author And Source
이 문제에 관하여([Programmers][JAVA] 프린터), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hye_b/ProgrammersJAVA-프린터저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)