Java/Programmers

[Lv.2] 프린터

GLOWWW 2023. 2. 19. 23:22

첫 설계 방법이다. 이대로 설계하니, 다행스럽게도 통과는 하였다. 다른 분들의 풀이를 보고 나니 더 쉽게 할 수 있었다. 우선 내가 풀이한 코드이다.

package Lv_2;

import java.util.*;

public class lv2test {
    public static int solution(int[] priorities, int location) {
        List<Integer> result = new ArrayList<>();
        List<Integer> result_idx = new ArrayList<>();
        LinkedList<Integer> pq = new LinkedList<>();
        LinkedList<Integer> idx = new LinkedList<>();
        for (int i = 0; i < priorities.length; i++) {
            pq.add(priorities[i]);
            idx.add(i);
        }
        int max_num = Integer.MIN_VALUE;
        for (int num : pq) {
            if (num > max_num) {
                max_num = num;
            }
        }
        while (!pq.isEmpty()) {
            for (int i = 0; i < pq.size(); i++) {
                if (pq.peek() < max_num) {
                    pq.add(pq.poll());
                    idx.add(idx.poll());
                } else if (pq.peek() == max_num) {
                    result.add(pq.poll());
                    result_idx.add(idx.poll());
                    if (pq.isEmpty()) {
                        break;
                    } else {
                        max_num = Integer.MIN_VALUE;
                        for (int num : pq) {
                            if (num > max_num) {
                                max_num = num;
                            }
                        }
                    }
                }
            }
        }
        return result_idx.indexOf(location) + 1;
    }

    public static void main(String[] args) {
        int[] priorities = {2, 1, 3, 2};
        int location = 2;
        int result = solution(priorities, location);
        System.out.println(result);
    }
}

풀고나서보니 어차피 result ArrayList값은 활용하지 않았다. 굳이 만들 필요도, add할 필요도 없었다.

다음은 다른분들의 풀이를 보고난 후, 우선순위 큐를 활용한 방법이다. 더 쉽고 간단하지만, 프로그래머스에서 원하는 스택/큐 방식의 풀이는 아닌것 같다고 생각한다. 우선순위 큐는 엄밀히 말하면 트리자료구조이기 때문이다.(맞나...?)

package Lv_2;

import java.util.*;

public class 프린터 {
    public static int solution(int[] priorities, int location) {
        int answer = 0;
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());

        for (int i = 0; i < priorities.length; i++) {
            priorityQueue.add(priorities[i]);
        }

        while (!priorityQueue.isEmpty()) {
            for (int i = 0; i < priorities.length; i++) {
                if (priorities[i] == priorityQueue.peek()) {
                    if (i == location) {
                        answer++;
                        return answer;
                    }
                    priorityQueue.poll();
                    answer++;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] priorities = {2, 1, 3, 2};
        int location = 2;
        int result = solution(priorities, location);
    }
}

아래의 링크는 우선순위 큐를 공부하고 정리해 본 링크이다.
https://sungsikyang92.tistory.com/241