티스토리 뷰
https://www.acmicpc.net/problem/1966
1966번: 프린터 큐
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에
www.acmicpc.net
const fs = require('fs');
const [n, ...arr] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const N = +n;
const docs =[];
for(let i = 0; i<N; i++){
const [num,target] = arr.shift().split(' ');
const priority = arr.shift().split(' ');
docs.push([num,target,priority]);
}
class Node{
constructor(item){
this.item = item;
this.next = null;
}
}
class Queue{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(item){
const node = new Node(item)
if(this.head===null){
this.head= node;
this.head.next = null;
}else{
this.tail.next = node;
}
this.tail = node;
this.length +=1;
}
pop(){
if(this.empty()==1) return -1
const popItem = this.head;
this.head = this.head.next;
this.length -=1;
return popItem.item;
}
size(){
return this.length;
}
empty(){
if(this.size()==0){
return 1;
}else{
return 0;
}
}
front(){
if(this.empty()==1) return -1;
return this.head.item;
}
back(){
if(this.empty()==1) return -1;
return this.tail.item;
}
}
let answer = [];
docs.forEach(v=>{
const [num,target,priority] = v;
let q = new Queue();
let priorityArr = [0,0,0,0,0,0,0,0,0,0];
for(let i = 0; i<num; i++){
q.push({index:i,priority:priority[i]});
priorityArr[priority[i]]++;
}
function check(p){
if(p==9) return true;
for(let i = 9; i>p; i--){
if(priorityArr[i]>0) return false;
}
return true;
}
let cnt = 0;
while(true){
let temp = q.pop();
if(check(temp.priority)){
cnt++;
if(temp.index==target){
answer.push(cnt)
break;
}else{
priorityArr[temp.priority]--;
}
}else{
q.push(temp);
}
}
})
console.log(answer.join('\n'))
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js)백준 1021번 : 회전하는 큐 ★ (0) | 2021.09.13 |
---|---|
Node.js) 백준 10866번 : 덱 (0) | 2021.09.13 |
Node.js) 백준 11866번 : 요세푸스 문제 0 (0) | 2021.09.13 |
Node.js) 백준 18258번 : 큐 2 (0) | 2021.09.13 |
Node.js) 백준 17298 : 오큰수 ★ (0) | 2021.09.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 개발자면접
- MySQL
- create db
- 면접질문
- DB 생성
- 서버점검
- 로드나인
- 다이나밍프로그래밍
- 면접비
- BFS
- 다이나믹프로그래밍
- 그래프
- 투포인터
- MOD
- 동적프로그래밍
- node.js
- 은둔청년체험
- 롱베케이션
- 투포인터 연습
- 서버개발
- create databases;
- 최소공통조상
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함