[프로그래머스]프린터(JAVA)

JESS YANG·2021년 5월 2일
0

프로그래머스

목록 보기
7/13
post-thumbnail

문제

https://programmers.co.kr/learn/courses/30/lessons/42587

내 코드

public  int solution(int[] priorities1, int location1) {
	int result = 0;
	//1
	Queue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
	
	for(int prioritie : priorities1) {
		pq.offer(prioritie);
	}
    //2
    while(!pq.isEmpty()) {
    	for(int i=0; i<priorities1.length; i++) {
    		if(pq.peek() == priorities1[i]) {
    			pq.poll();
    			result++;
        		if(location1 == i) {
        			pq.clear();
        			break;
        		}	
    		}
    	}
    	
    }
	
	return result;
}

풀이

  1. 역배열로 저장 시키는 PriorityQueue 생성하고 데이터를 담는다.
  2. 최우선순위를 제거해가면서 count를 세주고 해당 location과 반복문 i 가 같을때 while문을 빠져나온다.

new PriorityQueue(Collections.reverseOrder())로 생성할 경우 역배열 정렬 시켜준다.

0개의 댓글