[프로그래머스]스택/큐 - 프린터

·2021년 10월 1일
0

코테문제풀기

목록 보기
26/57

문제확인

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

문제풀이

function solution(priorities, location) {
    var answer = 0;
    let count = 0;

    while(priorities.length > 0){
        //1. 인쇄 대기목록의 가장 앞에 있는 문서(J)를 대기목록에서 꺼냅니다.
        let j = priorities.shift();
        //2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면 J를 대기목록의 가장 마지막에 넣습니다.
        if(priorities.some((prior) => {return prior > j;})) {
            priorities.push(j);
        //3. 그렇지 않으면 J를 인쇄합니다.
        } else {
            count++;
            if(location == 0) {
                answer = count;
                return answer;
            }
        }
        
        //변경되는 location 트래킹
        if(location == 0) {
            location = priorities.length - 1;
        } else {
            location -= 1;
        }
    }
}

0개의 댓글