[백준 10799] 스택 - 쇠막대기

김민지·2024년 1월 28일
0

냅다 시작 백준

목록 보기
113/118

✨ 문제 ✨


✨ 정답 ✨

// 값 입력받는 코드
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];
rl.on('line', function (line) {
  input.push(line);
}).on('close', function (){
  solution(input);
  process.exit();
});

// 풀이코드
function solution(input){
input2 = input[0].split('');
let answer=0;

let stack = [];
for (let i = 0; i < input2.length; i++) {
  if (input2[i] === '(') {
    if (input2[i + 1] === ')') {
      answer+=stack.length;
      i+=1;
    } else {
      stack.push('(');
    }
  }
  else{
    stack.pop();
    answer+=1;
  }
}
console.log(answer);
}

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

profile
이건 대체 어떻게 만든 거지?

0개의 댓글