프린터 (프로그래머스)

Namlulu·2022년 1월 13일
0

알고리즘

목록 보기
13/28
function solution(priorities, location) {
    const iterCount = priorities.length
    let loc = location
    let count = 1
    
    while (1) {
        if (priorities[0] >= Math.max(...priorities.slice(1))) {
            if (loc === 0) {
                break
            }
            
            priorities.shift()
            
            count += 1
            loc -= 1
        } else {
            const first = priorities.shift()
            priorities.push(first)
            
            if (loc === 0) {
                loc = priorities.length - 1
            } else {
                loc -= 1
            }
            
        }
    }
    
    
    return count
}

=> queue를 활용하여 구한 문제이다. node는 queue 내장 모듈이 없어서 그냥 배열로 구하였다. 1차시도에는 while문을 활용하지 않고 for문으로 연산하여 요소의 마지막까지 구하지 못하였다. 이 점을 캐치해서 2차 시도 끝에 통과하였다.

profile
Better then yesterday

0개의 댓글