PS/프로그래머스

[Level2][Javascript] 프린터

returnzero 2022. 6. 4. 15:56
function solution(priorities, location) {
    var answer = 0;
    let index= priorities.length;
    while(priorities.length!==0){
        if ( Math.max.apply(null, priorities) !== priorities[0]){
            priorities.push(priorities.shift());
            if(location===0) {location=priorities.length-1;}
            else {location--;}
            }
        else{
            priorities.shift();
            answer++;
            if (location==0)return answer;
            location--;
        }
        }    
    
    }

코드가 어렵지 않다. 최대한 빨리 생각하자. 

* Math.max.apply(null, array) 하게 되면 배열의 최대값이 나오는건 유용하게 사용가능할듯

* array.length의 return은 마지막 index가 아닌 실제 길이 값임에 유의