Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions howoo/주식각격.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function solution(prices) {
const n = prices.length;
const answer = new Array(n).fill(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 저는 아직 new Array 사용하는게 너무 안 익숙해요 ㅋㅋㅋ
스택이나 DFS 할 때도 다른 풀이 보면 많이들 사용하시더라구요


// 특정 조건의 prices의 인덱스를 담는 스택
const stack = [];

for (let i = 0; i < n; i++) {
// 스택의 길이가 0보다 크고, 현재 선택한 가격보다 스택의 마지막 요소의 인덱스의 가격이 크면(가격이 떨어졌다는 의미)
while (stack.length > 0 && prices[stack[stack.length - 1]] > prices[i]) {
// 가격이 떨어진 인덱스를 없에고 변수에 담음
const idx = stack.pop();
// 정답 배열에 현재 i에서 가격이 떨어진 인덱스의 가격을 빼서 저장
answer[idx] = i - idx;
}
stack.push(i);
}
Comment on lines +8 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 이해 못하고 있었는데 정우님 주석 보니까 대략 알 것도 같습니다.. 재도전..


// 위 단계 까지 진행하면
// stack에는 [0, 1, 3, 4]
// answer에는 [0, 0, 1, 0, 0]
// 이 담겨 있음

// answer에 0이 담겼다는 것은 가격이 내려가지 않았다는 의미

while (stack.length > 0) {
const idx = stack.pop();
answer[idx] = n - idx - 1;
}

return answer;
}

console.log(solution([1, 2, 3, 2, 3]));