[코딩테스트] 프린터

시나브로·2021년 6월 14일
0

코딩테스트

목록 보기
5/34
post-thumbnail

문제


프린터 문제 바로가기



제출 코드(Java)


첫번째 코드 제출

import java.util.*;

class Solution {
    
    public int solution(int[] priorities, int location) {
        Queue<Print> queue = new ArrayDeque<>();

        for (int i = 0; i < priorities.length; i++) {
            Print printElement = new Print()
                    .setPriority(priorities[i])
                    .setResult(i == location);
            queue.add(printElement);
        }

        Arrays.sort(priorities);

        int result = 1;
        int idx = priorities.length - 1;

        while (!queue.isEmpty()) {
            Print print = queue.poll();

            if ( priorities[idx] > print.getPriority() ) {
                queue.add(print);
            } else {
                if( print.isResult() ) break;
                idx --;
                result++;
            }
        }

        return result;
    }
    
    public class Print{

        int priority;
        boolean result;


        public int getPriority() {
            return priority;
        }

        public Print setPriority(int priority) {
            this.priority = priority;
            return this;
        }

        public boolean isResult() {
            return result;
        }

        public Print setResult(boolean result) {
            this.result = result;
            return this;
        }

        @Override
        public String toString() {
            return "Print{" +
                    "priority=" + priority +
                    ", result=" + result +
                    '}';
        }
    }
}

저번 문제에서 사용했던 inner Class와 Queue를 사용하여 해결한 문제


정확성 테스트

정확성  테스트
테스트 1 〉	통과 (0.78ms, 51.6MB)
테스트 2 〉	통과 (1.09ms, 52.1MB)
테스트 3 〉	통과 (0.83ms, 54.2MB)
테스트 4 〉	통과 (0.78ms, 51.8MB)
테스트 5 〉	통과 (0.74ms, 53.4MB)
테스트 6 〉	통과 (0.80ms, 52.2MB)
테스트 7 〉	통과 (0.75ms, 53MB)
테스트 8 〉	통과 (1.19ms, 53.9MB)
테스트 9 〉	통과 (0.70ms, 53.6MB)
테스트 10 〉	통과 (1.50ms, 55.5MB)
테스트 11 〉	통과 (1.00ms, 51.9MB)
테스트 12 〉	통과 (0.80ms, 52.6MB)
테스트 13 〉	통과 (0.99ms, 53MB)
테스트 14 〉	통과 (0.75ms, 53.2MB)
테스트 15 〉	통과 (0.69ms, 52.8MB)
테스트 16 〉	통과 (0.72ms, 51.7MB)
테스트 17 〉	통과 (2.07ms, 54.7MB)
테스트 18 〉	통과 (0.76ms, 52.4MB)
테스트 19 〉	통과 (1.07ms, 52.6MB)
테스트 20 〉	통과 (1.36ms, 52.4MB)




처음에 우선순위 큐를 사용하여 했다가 정렬과정에서 이슈가 생겨 애먹었던 문제. Tree 구조의 노드 정렬을 잊고 있었다,,



제출 코드(Python)


코드 제출

def solution(priorities, location):

    sortedPriorities = priorities.copy()
    sortedPriorities.sort(reverse=True)
    result = 1

    while len(priorities) > 0:
        tmpElement = priorities.pop(0)
        if tmpElement < sortedPriorities[0]:
            priorities.append(tmpElement)
            location -= 1
        else:
            if location == 0:
                return result
            else:
                sortedPriorities.pop(0)
                result += 1
                location -= 1

        location = len(priorities)-1 if location < 0 else location

기본 list에도 Queue처럼 사용할 수 있었다.


정확성 테스트

정확성  테스트
테스트 1 〉	통과 (0.03ms, 10.2MB)
테스트 2 〉	통과 (0.20ms, 10.2MB)
테스트 3 〉	통과 (0.02ms, 10.2MB)
테스트 4 〉	통과 (0.01ms, 10.2MB)
테스트 5 〉	통과 (0.01ms, 10.2MB)
테스트 6 〉	통과 (0.05ms, 10.2MB)
테스트 7 〉	통과 (0.03ms, 10.2MB)
테스트 8 〉	통과 (0.12ms, 10.2MB)
테스트 9 〉	통과 (0.02ms, 10.2MB)
테스트 10 〉	통과 (0.05ms, 10.2MB)
테스트 11 〉	통과 (0.12ms, 10.3MB)
테스트 12 〉	통과 (0.01ms, 10.3MB)
테스트 13 〉	통과 (0.12ms, 10.2MB)
테스트 14 〉	통과 (0.01ms, 10.2MB)
테스트 15 〉	통과 (0.01ms, 10.2MB)
테스트 16 〉	통과 (0.03ms, 10.2MB)
테스트 17 〉	통과 (0.15ms, 10.2MB)
테스트 18 〉	통과 (0.01ms, 10.3MB)
테스트 19 〉	통과 (0.10ms, 10.2MB)
테스트 20 〉	통과 (0.03ms, 10.2MB)

차후 Queue를 다시 사용하게 된다면 Deque()도 있으니 참고해서 사용해봐야겠다.



profile
Be More!

0개의 댓글