ES5 프로토타입 상속 문법
export function FQueue() {
this.inputSt = [];
this.outputSt = [];
this.size = 0;
this.enque = (value) => {
this.inputSt.push(value);
this.size++;
}
this.deque = () => {
if(this.outputSt.length === 0) {
this.outputSt = this.inputSt.reverse();
this.inputSt = [];
}
const top = this.outputSt[this.outputSt.length-1];
this.outputSt.pop();
this.size--;
return top;
}
}
export class CQueue {
constructor() {
this.inputSt = [];
this.outputSt = [];
this.size = 0;
}
enque(value) {
this.inputSt.push(value);
this.size++;
}
deque() {
if(this.outputSt.length === 0) {
this.outputSt = this.inputSt.reverse();
this.inputSt = [];
}
const top = this.outputSt[this.outputSt.length-1];
this.outputSt.pop();
this.size--;
return top;
}
}
import {FQueue} from "./FQueue.js"
import {CQueue} from "./CQueue.js";
const fQue = new FQueue();
const cQue = new CQueue();
const input = [1,2,3,4,5];
console.log('-------------FQueue-------------------');
input.forEach(val => fQue.enque(val));
while(fQue.size > 0) {
fQue.deque();
}
console.log('--------------------------------------');
console.log('-------------CQueue-------------------');
input.forEach(val => cQue.enque(val));
while(cQue.size > 0) {
cQue.deque();
}
console.log('--------------------------------------');
현재 대기목록에 있는 문서의 중요도가 순서대로 담긴 배열 priorities와 내가 인쇄를 요청한 문서가 현재 대기목록의 어떤 위치에 있는지를 알려주는 location이 매개변수로 주어질 때, 내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 return 하도록 solution 함수를 작성해주세요.
class MyQueue {
constructor() {
this.inputSt = [];
this.outputSt = [];
this.size = 0;
}
enque(value) {
this.inputSt.push(value);
this.size++;
}
deque() {
if(this.outputSt.length === 0) {
this.outputSt = this.inputSt.reverse();
this.inputSt = [];
}
const top = this.outputSt[this.outputSt.length-1];
this.outputSt.pop();
this.size--;
return top;
}
}
function solution(priorities, location) {
const que = new MyQueue();
priorities.forEach((p, loc) => que.enque([p, loc]));
priorities.sort((a,b) => b-a);
const printLoc = [];
let printedCnt = 0;
while(que.size > 1) {
const [peek, loc] = que.deque();
if(peek < priorities[printedCnt]) {
que.enque([peek, loc]);
continue;
}
printedCnt++;
printLoc.push(loc);
}
return printLoc.indexOf(location)+1;
}