๐ŸŽฒ ๋ฐฑ์ค€ 17298๋ฒˆ ์˜คํฐ์ˆ˜

Jeongeunยท2024๋…„ 2์›” 15์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
166/187

๐Ÿ“ฃ ๋ฌธ์ œ

๐Ÿงธ ํƒ‘๊ณผ ๊ฐ™์€ ์œ ํ˜•์˜ ๋ฌธ์ œ์ด๋‹ค.

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const nums = input[0].split(" ").map((el, index) => [index, +el]);
const stack = [];
stack.push(nums[0]);
const answer = new Array(N).fill(-1);

for (let i = 1; i < N; i++) {
  while (stack.length) {
    if (stack[stack.length - 1][1] < nums[i][1]) {
      answer[stack[stack.length - 1][0]] = nums[i][1];
      stack.pop();
    } else break;
  }
  stack.push(nums[i]);
}

console.log(answer.join(" "));

0๊ฐœ์˜ ๋Œ“๊ธ€