문제링크
1966번: 프린터 큐
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에
www.acmicpc.net
문제풀이
주요한 아이디어는 [인덱스, 우선순위] 이렇게 배열의 형태로 큐에 집어 넣은것이다. (사실 배열로 만들어서 큐라고 할 수없지만..)
const fs = require("fs");
BOJkey = 0;
let input = fs
.readFileSync(BOJkey ? "./자바스크립트로/1966/input.txt" : "./dev/stdin")
.toString()
.trim()
.split("\n");
let testCaseNumber = input.shift();
let num = 0;
let findIndex = 0;
let arr = [];
let count;
let target;
let ans = 0;
let result = "";
for (let i = 0; i < testCaseNumber; i++) {
count = 0;
test = input
.shift()
.split(" ")
.map((v) => +v);
num = test[0];
findIndex = test[1];
priority = input
.shift()
.split(" ")
.map((v) => +v);
for (let j = 0; j < num; j++) {
arr.push([j, priority[j]]);
}
priority.sort((a, b) => b - a);
priority.map((el) => {
while (true) {
target = arr.shift();
if (el == target[1]) {
count++;
if (findIndex == target[0]) ans = count;
break;
} else arr.push(target);
}
});
result += `${ans}\n`;
}
console.log(result);
'공부기록 > 자바스크립트 코딩테스트' 카테고리의 다른 글
자바스크립트 in 연산자 (0) | 2022.06.30 |
---|---|
자바스크립트 중괄호 괄호의 기능 (0) | 2022.06.30 |
[백준/JS] 10799 쇠막대기 (0) | 2022.06.29 |
[백준/JS] 1935 후위표기식2 (0) | 2022.06.29 |
중위표기법 후위표기법 변환 (0) | 2022.06.29 |