(Python, Java) 프로그래머스 - 프린터

[문제 링크]

Python 풀이

from collections import deque


def solution(priorities, location):
    answer = 0
    q = deque()
    for idx, priority in enumerate(priorities):
        q.append((priority, idx))

    while q:
        priority, idx = q.popleft()
        if len(q) != 0 and priority < sorted(q)[-1][0]:
            q.append((priority, idx))
        else:
            answer += 1
            if idx == location:
                break

    return answer

제한 범위도 여유롭고 문제에 어떻게 풀라는지도 나와있어서 level1에 있도도 될만한 문제같다.


Python 풀이 2

def solution(priorities, location):
    queue =  [(i,p) for i,p in enumerate(priorities)]
    answer = 0
    while True:
        cur = queue.pop(0)
        if any(cur[1] < q[1] for q in queue):
            queue.append(cur)
        else:
            answer += 1
            if cur[0] == location:
                return answer

다른 분의 풀이 중에 any를 사용한 것이 있어서 가져왔다.
any는 모르는 내장 함수여서 배울 필요가 있었다.

any, all

  • any()
    • 인수 중 하나라도 True가 있으면 True 반환
  • all()
    • 인수 모두가 True 여야 True 반환
>>> any([False, False, False])
False
>>> any([False, True, False])
True
>>> all([False, True, False])
False
>>> all([True, True, True])
True

Java 풀이

import java.util.LinkedList;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        LinkedList<Print> prints = new LinkedList<>();

        for (int idx = 0; idx < priorities.length; idx++) {
            prints.add(new Print(priorities[idx], idx));
        }

        while (!prints.isEmpty()) {
            Print print = prints.pollFirst();
            if (prints.isEmpty()) {
                answer++;
                break;
            }

            Integer max = prints.parallelStream().map(Print::getPriority).max(Integer::compareTo).get();
            if (print.getPriority() < max) {
                prints.add(print);
            } else {
                answer++;
                if (print.getLocation() == location) {
                    break;
                }
            }

        }

        return answer;
    }

    class Print {
        int priority;
        int location;

        public Print(int priority, int location) {
            this.priority = priority;
            this.location = location;
        }

        public int getPriority() {
            return priority;
        }

        public int getLocation() {
            return location;
        }
    }
}

© 2021. By Backtony