-
Notifications
You must be signed in to change notification settings - Fork 5
[pgm] 주식각격 / level2 / 못품 #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ollykingking
wants to merge
1
commit into
kledyu:main
Choose a base branch
from
ollykingking:호우화요일
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\uD638\uC6B0\uD654\uC694\uC77C"
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| // 특정 조건의 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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])); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 저는 아직 new Array 사용하는게 너무 안 익숙해요 ㅋㅋㅋ
스택이나 DFS 할 때도 다른 풀이 보면 많이들 사용하시더라구요