2493. 탑 - node.js / javascript

윤상준·2022년 4월 21일
0

BOJ - node.js / javascript

목록 보기
34/55
post-thumbnail

문제

내 코드

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");

const N = Number(input[0]);
let towers = input[1].split(" ");
const stack = [];
const answer = [];

for (let i = 0; i < N; i++) {
  const currentTower = {
    index: i + 1,
    height: Number(towers[i]),
  };

  if (!stack.length) {
    stack.push(currentTower);
    answer.push(0);
    continue;
  }

  if (stack[stack.length - 1].height < currentTower.height) {
    while (stack.length) {
      if (stack[stack.length - 1].height > currentTower.height) {
        break;
      } else {
        stack.pop();
      }
    }

    !stack.length ? answer.push(0) : answer.push(stack[stack.length - 1].index);
    stack.push(currentTower);
  } else {
    answer.push(stack[stack.length - 1].index);
    stack.push(currentTower);
  }
}

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

깃허브 링크

https://github.com/highjoon/Algorithm/blob/master/BOJ/2493.js

profile
하고싶은건 많은데 시간이 없다!

0개의 댓글