T
N
๊ณผ ๋ชฉํ ๋ฌธ์์ ์ธ๋ฑ์ค M
์ ์ฐจ
const fs = require('fs');
const path = require('path');
const input = fs.readFileSync(path.join(__dirname, 'input.txt')).toString().trim().split('\\n');
// ๋ฐฐ์ด ๋ถํด
let [n, ...arr] = input;
// n์ ํ
์คํธ ์, ๋๋จธ์ง๋ ํ
์คํธ ์ ๋ณด
// ๋ฌธ์์ด ๋ฐฐ์ด์ ์ซ์ ๋ฐฐ์ด๋ก ๋ณํ
arr = arr.map((item) => item.split(' ').map(Number));
let answer = '';
for (let i = 0; i < arr.length; i += 2) {
let count = 0; // ์ถ๋ ฅ๋ ๋ฌธ์์ ์
const priorities = arr[i + 1]; // ํ์ฌ ํ
์คํธ์ ์ฐ์ ์์ ๋ฐฐ์ด
let location = arr[i][1]; // ๋ชฉํ ๋ฌธ์์ ์ด๊ธฐ ์์น
while (true) {
// ํ์ฌ ํ์์ ๊ฐ์ฅ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง ๋ฌธ์๋ฅผ ์ฐพ๊ธฐ
const max = Math.max(...priorities);
// ํ ์์์ ๋ฌธ์ ํ๋ ๊บผ๋ด๊ธฐ
const number = priorities.shift();
// ๊บผ๋ธ ๋ฌธ์์ ์ค์๋(number)๊ฐ ์ต๋ ์ค์๋(max) ๋น๊ต
if (number === max) {
count++;
// ๋ชฉํ ๋ฌธ์์ ์์น๊ฐ 0์ผ ๊ฒฝ์ฐ
if (location === 0) {
//ํ์ฌ ์ถ๋ ฅ๋ ๋ฌธ์ ์(count)๋ฅผ ์ ๋ต ๋ฐฐ์ด์ ์ถ๊ฐ
answer += count + '\\n';
break;
}
} else {
// ๊บผ๋ธ ๋ฌธ์์ ์ค์๋๊ฐ ์ต๋๋ณด๋ค ๋ฎ์ผ๋ฉด ํ์ ๋งจ ๋ค์ ๋ค์ ์ถ๊ฐ
priorities.push(number);
}
// ํ์ฌ ๋ฌธ์์ ์์น๊ฐ 0์ผ ๊ฒฝ์ฐ
if (location === 0) {
// ํ์์ ๋ง์ง๋ง ๋ฌธ์๋ก ์์น ์ฎ๊ธฐ๊ธฐ
location = priorities.length - 1;
} else {
// ๊ทธ๋ ์ง ์์ผ๋ฉด ๋จ์ํ 1 ๊ฐ์
location--;
}
}
}
console.log(answer.trim());