diff --git a/src/bj/gold/5/6198.js b/src/bj/gold/5/6198.js new file mode 100644 index 00000000..d5713bad --- /dev/null +++ b/src/bj/gold/5/6198.js @@ -0,0 +1,30 @@ +"use strict"; + +/** + * [stack 문제] + * - stack의 길이를 이용해 푸는 문제이다. 생각보다 어려운 스택.. + * cf. (https://velog.io/@thguss/%EB%B0%B1%EC%A4%80-6198.-%EC%98%A5%EC%83%81-%EC%A0%95%EC%9B%90-%EA%BE%B8%EB%AF%B8%EA%B8%B0-with.-Python) + */ +const input = require("fs") + .readFileSync("/dev/stdin") + .toString() + .trim() + .split("\n"); + +const solution = (input) => { + const N = +input[0]; + const stack = []; + let answer = 0; + + for (let i = 1; i <= N; i++) { + const num = +input[i]; + while (stack.length !== 0 && stack[stack.length - 1] <= num) { + stack.pop(); + } + stack.push(num); + answer += stack.length - 1; + } + return answer; +}; + +console.log(solution(input)); diff --git a/src/bj/gold/5/README.md b/src/bj/gold/5/README.md index 0bf205ab..20eff294 100644 --- a/src/bj/gold/5/README.md +++ b/src/bj/gold/5/README.md @@ -25,3 +25,4 @@ | 21 | 21758 | [꿀 따기](./21758.js) | 23.06.10 | X | | 22 | 9205 | [맥주 마시면서 걸어가기](./9205.js) | 23.06.20 | [23.06.21](./replay/9205.js) | | 23 | 2493 | [탑](./2493.js) | 23.06.28 | △ | +| 24 | 6198 | [옥상 정원 꾸미기](./6198.js) | 23.06.29 |