Skip to content

Commit e00f4cd

Browse files
committed
Stock Price / 심화
1 parent b2fdfc6 commit e00f4cd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function solution(prices) {
2+
const answer = Array(prices.length).fill(0);
3+
const stack = [];
4+
5+
prices.forEach((price, i) => {
6+
while (stack.length && prices[stack[stack.length - 1]] > price) {
7+
const idx = stack.pop();
8+
answer[idx] = i - idx;
9+
}
10+
stack.push(i);
11+
});
12+
13+
stack.forEach((idx) => {
14+
answer[idx] = prices.length - 1 - idx;
15+
});
16+
17+
return answer;
18+
}

0 commit comments

Comments
 (0)