[백준 1874번]스택 수열

[백준 1874번]스택 수열

문제

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

문제풀이

주어진 정수값이 0으로 초기화 된 max보다 클 경우에 해당 정수값까지 push 연산

해당 정수값까지 push된 stack 배열 내에서 마지막 인덱스 요소인 주어진 정수값을 pop 연산

max값을 주어진 정수값으로 초기화

주어진 정수값이 이전 정수값으로 초기화 된 max값보다 작을 경우에 해당 정수값까지 pop연산

코드

function solution(arr) { let answer = []; let stack = []; let tmp = []; let max = 0; let newArr = arr.filter((v, i) => i !== 0).map(val => Number(val)) for(let x of newArr) { if(max < x) { for(let i = max; i < x; i++) { stack.push(i + 1); answer.push('push'); } tmp.push(stack.pop()); answer.push('pop'); max = x; } else { while(stack[stack.length - 1] >= x) { tmp.push(stack.pop()); answer.push('pop'); } } } if(newArr.join('') !== tmp.join('')) return answer = 'NO'; return answer.join('

'); } let arr = ['8', '4', '3', '6', '8', '7', '5', '2', '1']; console.log(solution(arr));

위에 로직이 외부 편집기에서는 맞는 것으로 확인되었지만 nodeJS 런타임 환경에서는 틀렸다고 나온다.

테스트 케이스 중 걸맞지 않은 사항이 있거나 프로그램 자체의 오류일 것 같은데, 향후 로직을 다시 생각해 본 뒤 도전해 보고자 한다.

from http://choi95.tistory.com/147 by ccl(A) rewrite - 2021-08-27 13:26:38