function solution(priorities, location) {
// 인쇄되는 횟수 측정 변수
let count = 0
// 인쇄를 원하는 대기목록의 요소(동일한 요소들이 있어 구별해내기 위해 문자열로 형변환)
priorities[location] = priorities[location] + ''
while(1) {
if(Number(priorities[0]) === Math.max(...priorities)) {
count++
if(typeof priorities[0] === 'string') {
break;
}
priorities.shift()
}
else {
let notPrint = priorities[0]
priorities.shift()
priorities.push(notPrint)
}
}
return count
}
위 코드를 간단하게 요약하자면
function solution(priorities, location) {
const arr = priorities.map((item, index) =>
({priority:item, index}))
const queue = []
while(arr.length > 0) {
const headPriority = arr.shift()
const isHighPriority = arr.some(el => el.priority > headPriority.priority)
if(isHighPriority) {
arr.push(headPriority)
}
else {
queue.push(headPriority)
}
}
return queue.findIndex(el => el.index === location) + 1
}
class Queue {
constructor() {
this.queue = [];
this.front = 0;
this.rear = 0;
}
enqueue(value) {
this.queue[this.rear++] = value;
}
dequeue() {
const value = this.queue[this.front];
delete this.queue[this.front];
this.front += 1;
return value;
}
peek() {
return this.queue[this.front];
}
size() {
return this.rear - this.front;
}
}
class Node {
constructor(value){
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
enqueue(newValue) {
const newNode = new Node(newValue);
if (this.head === null) {
this.head = this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.size += 1;
}
dequeue() {
const value = this.head.value;
this.head = this.head.next;
this.size -= 1;
return value;
}
peek() {
return this.head.value;
}
}