File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ // 1. 연속성을 체크하는 문제이면 스택 의심하기.
2+ // 가격이 떨어지지 않은 연속기간을 찾는 문제!, 연속된 값들의 관계를 추적할 때 스택이 용이하다.
3+ // 2. 이전 상태를 기억해야하는지
4+ // 현재 가격을 이전의 가격들과 계속 비교해야함.
5+ // 3. 나중에 들어온 데이터가 이전 데이터에 영향을 주는지
6+ // 현재 시점의 가격이 이전시점들의 '가격 유지 기간'에 영향을 준다 -> 스택으로 이전데이터 보관하기
7+ // => 연속성, 이전상태 기억, 나중데이터가 이전데이터에 영향을 주는 것 => 3가지를 체크하자 => 스택/큐
8+ function solution ( prices ) {
9+ const stack = [ ] ; // {price: 가격, time: 시점} 객체를 저장할 스택
10+ const totalSeconds = prices . length ;
11+ const maintainedSeconds = Array ( totalSeconds ) . fill ( 0 ) ; // 각 시점별 가격이 유지된 기간
12+
13+ for ( let currentTime = 0 ; currentTime < totalSeconds ; currentTime ++ ) {
14+ const currentPrice = prices [ currentTime ] ;
15+
16+ while ( stack . length && stack . at ( - 1 ) . price > currentPrice ) {
17+ const lastPrice = stack . pop ( ) ;
18+ maintainedSeconds [ lastPrice . time ] = currentTime - lastPrice . time ;
19+ }
20+
21+ stack . push ( { price : currentPrice , time : currentTime } ) ;
22+ }
23+
24+ // 끝까지 가격이 떨어지지 않은 주식들 처리
25+ while ( stack . length ) {
26+ const remainingPrice = stack . pop ( ) ;
27+ maintainedSeconds [ remainingPrice . time ] =
28+ totalSeconds - 1 - remainingPrice . time ;
29+ }
30+
31+ return maintainedSeconds ;
32+ }
You can’t perform that action at this time.
0 commit comments