레퍼런스 풀이
function solution(s) {
const stack = []
s.split(' ').forEach((target) => {
if(target === 'Z') stack.pop();
else stack.push(+target)
})
return stack.length ? stack.reduce((pre, cur) => pre + cur) : 0;
}
스택 자료구조 활용법을 참고하자.
'알고리즘' 카테고리의 다른 글
내 즙을 짜낸 Greatest Common Divisor of Strings (0) | 2023.03.29 |
---|---|
그리디 알고리즘(탐욕법) (2) | 2023.03.21 |
프로그래머스 lv.0 인덱스 바꾸기 (0) | 2023.02.27 |
프로그래머스 lv.0 피자 나눠 먹기(2), (3) (0) | 2023.02.24 |
프로그래머스 lv.0 분수의 덧셈 (0) | 2023.02.22 |