백준-Node.js-10773, 제로

송철진·2023년 3월 5일
0

백준-Node.js

목록 보기
46/69

문제

https://www.acmicpc.net/problem/10773

풀이

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim()
                .split('\n').slice(1).map(Number)

const solution = input => {
  const stack = [];
  input.forEach(el => el !== 0 ? stack.push(el) : stack.pop());
  return stack.reduce((a,b)=>a+b,0)
}
console.log(solution(input))

input을 순회하여 0이 아니면 stack에 요소를 추가하고 0이면 마지막값을 삭제한다.
순회를 종료하면 stack의 값을 합산하여 반환한다

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글