seunghyun Note
[백준] 10773 : 제로 (스택 공부 부수기) with JS 본문
728x90
반응형
링크 : https://www.acmicpc.net/problem/10773
10773번: 제로
첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경
www.acmicpc.net
문제 풀이
stack의 pop과 push로만 해결할 수 있다!
const fs = require("fs");
//const input = require("fs").readFileSync("/dev/stdin").toString().split("\n");
const input = fs.readFileSync("예제.txt").toString().trim().split("\n");
const [n, ...arr] = input;
const stack = [];
for (let i = 0; i < n; i++) {
let value = parseInt(arr[i].split(" "));
if (value !== 0) stack.push(value);
else stack.pop();
}
let sum = stack.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
console.log(sum);
입력값을 가져오는게 더 어려운거 같다.
728x90
반응형
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[백준] 4949번 : 균형잡힌 세상(스택 공부 부수기) with JS (0) | 2024.01.18 |
---|---|
[백준] 9012번 : 괄호 (스택 공부 부수기) with JS (0) | 2024.01.18 |
[백준] 28278번 : 스택 2 (스택 공부 부수기) with JS (0) | 2024.01.18 |
[프로그래머스] 기능개발(스택 공부 부수기) with JS (1) | 2024.01.17 |
[프로그래머스] 전화번호 목록 (해시) with JS (0) | 2024.01.17 |