Programmers

Programmers LV.2 프린터

Developer Garam.Choi 2020. 12. 27. 05:02

 

 

지문을 읽으면읽을수록 참 이해가 잘안되는거같다 

 

처음엔 

 

아래와 같이 개발했다가 테스트케이스 단위에서 정확성에서 실패 두두두두두

import java.util.*;
class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        class Print{
            Integer priorite = null;
            Integer location_priorite = null;

            public Print(Integer priorite,Integer location_priorite){
                this.priorite = priorite;
                this.location_priorite = location_priorite;
            }

            public Integer getPriorite() {
                return priorite;
            }

            public void setPriorite(Integer priorite) {
                this.priorite = priorite;
            }

            public Integer getLocation_priorite() {
                return location_priorite;
            }

            public void setLocation_priorite(Integer location_priorite) {
                this.location_priorite = location_priorite;
            }
        }

        Queue<Print> sortPrioritie = new LinkedList<>();

        List<Print> prioriteList = new ArrayList<>();
        for(int b=0; b<priorities.length; b++){
            sortPrioritie.offer(new Print(priorities[b], b));
        }


        for(int a =0; a < priorities.length; a++){
            for(int b =a+1; b < priorities.length; b++){
                    if(priorities[a] < priorities[b]){
                        sortPrioritie.offer(sortPrioritie.poll());
                        break;
                    }
            }
        }

        int c = 1;
        for(Print print : sortPrioritie){
            if(print.location_priorite ==location ){
                System.out.println(answer);
                answer = c;
            }
            c++;
        }
        return answer;
    }
}

 

위처럼하다가 너무 미궁에 빠져서

 

Array<Integer>로 id로 빼서 collection sort로 했다가, 그렇게하면 또 우선 값만 소팅하다보니깐 큐를 무조건써야해서

 

다른 분 코드를 참고해서 풀었다.

 

큐쪽은 지문 그대로 읽고 개발하다간 머리가 복잡해지는거같다.

 

import java.util.*;
class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        PriorityQueue<Integer> que = new PriorityQueue<>(Collections.reverseOrder());
        for(int b=0; b<priorities.length; b++){
            que.offer(priorities[b]);
        }

        while (!que.isEmpty()){

            for(int a=0; a < priorities.length; a++){
                if(que.peek() == priorities[a]){
                    que.poll();
                    answer++;
                    if(location == a){
                        que.clear();
                        break;
                    }
                }
            }
        }
         return answer;
    }  
}