Baekjoon

Baekjoon 10799번 쇠막대기

ppwag 2022. 3. 14. 23:46

문제

https://www.acmicpc.net/problem/10799'

걸린 시간

-

풀이

TypeScript

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input';
const stdin = fs.readFileSync(filePath).toString().trim().split('\n').map((s: string) => s.trim());
const input = (() => {
  let line = 0;
  return () => stdin[line++];
})();

interface Pair {
  index: number;
  value: string;
}

const bar:string = input();

let ans: number = 0;
let stack: Array<Pair> = [];

for(let i = 0; i < bar.length; i++){
  const item: Pair = {index: i, value: bar[i]};
  if(bar[i] === '(') stack.push(item);
  else{
    const top: Pair = stack[stack.length-1];
    stack.pop();
    if(top.index === item.index-1) ans += stack.length;
    else ans += 1;
  }
}
console.log(ans);

interface 가 c, c++ 의 구조체 느낌인가?

'Baekjoon' 카테고리의 다른 글

Baekjoon 1913번 달팽이  (0) 2022.03.29
Baekjoon 1904번 01타일  (0) 2022.03.16
Baekjoon 2480번 주사위 세개  (0) 2022.03.03
Baekjoon 3221번 개미의 이동  (0) 2021.09.30
Baekjoon 11725번 트리의 부모 찾기  (0) 2021.09.17

댓글