배꼽파지 않도록 잘 개발해요

99클럽 코테 스터디 28일차 TIL - 프로세스 본문

코딩테스트/99클럽

99클럽 코테 스터디 28일차 TIL - 프로세스

꼽파 2024. 8. 19. 10:50


function solution(priorities, location) {
    // 큐를 우선순위와 인덱스를 포함하는 배열로 반환
    let queue = priorities.map((priority, idx) => ({ priority, idx }));
    console.log(queue)
// [
//   { priority: 2, idx: 0 },
//   { priority: 1, idx: 1 },
//   { priority: 3, idx: 2 },
//   { priority: 2, idx: 3 }
// ]
    let printOrder = 0; // 몇 번째로 실행되는지 카운트
    
    while (queue.length > 0) {
        let current = queue.shift();
        
        // 현재 프로세스보다 높은 우선순위의 프로세스가 있는지 확인
        if (queue.some(process => process.priority > current.priority)) {
            queue.push(current)
        } else {
            printOrder++;
            if (current.idx === location) {
                return printOrder;
            }
        }
    }
}

 

728x90