๐งธ ์ฒ์์ ๋ฌธ์ ๋ฅผ ์๋ชป์ฝ๊ณ ์์ ๊ฐ์๋ฅผ ์ธ๊ณ ์์๋ค. ์๋ชป๋จ์ ๊นจ๋ซ๊ณ tops๋ฅผ [index,๋์ด]์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด ํด๊ฒฐํ๋ค.
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const tops = input[0]
.split(" ")
.map((el, index) => [index, +el])
.reverse();
const answer = new Array(N).fill(0);
const stack = [];
stack.push(tops[0]);
for (let i = 1; i < N; i++) {
while (stack.length) {
if (stack[stack.length - 1][1] < tops[i][1]) {
const top = stack.pop();
answer[top[0]] = N - i;
} else break;
}
stack.push(tops[i]);
}
console.log(
answer.join(" ")
);