File tree Expand file tree Collapse file tree 2 files changed +119
-0
lines changed
Expand file tree Collapse file tree 2 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1+ # [ level 2] 주식가격 - 42584
2+
3+ [ 문제 링크] ( https://school.programmers.co.kr/learn/courses/30/lessons/42584 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 72.8 MB, 시간: 49.23 ms
8+
9+ ### 구분
10+
11+ 코딩테스트 연습 > 스택/큐
12+
13+ ### 채점결과
14+
15+ 정확성: 66.7<br />효율성: 33.3<br />합계: 100.0 / 100.0
16+
17+ ### 제출 일자
18+
19+ 2025년 09월 02일 13:24:08
20+
21+ ### 문제 설명
22+
23+ <p >초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요.</p >
24+
25+ <h5 >제한사항</h5 >
26+
27+ <ul >
28+ <li >prices의 각 가격은 1 이상 10,000 이하인 자연수입니다.</li >
29+ <li >prices의 길이는 2 이상 100,000 이하입니다.</li >
30+ </ul >
31+
32+ <h5 >입출력 예</h5 >
33+ <table class =" table " >
34+ <thead><tr>
35+ <th >prices</th >
36+ <th >return</th >
37+ </tr >
38+ </thead >
39+ <tbody><tr>
40+ <td >[1, 2, 3, 2, 3]</td >
41+ <td >[4, 3, 1, 1, 0]</td >
42+ </tr >
43+ </tbody >
44+ </table>
45+ <h5 >입출력 예 설명</h5 >
46+
47+ <ul >
48+ <li >1초 시점의 ₩1은 끝까지 가격이 떨어지지 않았습니다.</li >
49+ <li >2초 시점의 ₩2은 끝까지 가격이 떨어지지 않았습니다.</li >
50+ <li >3초 시점의 ₩3은 1초뒤에 가격이 떨어집니다. 따라서 1초간 가격이 떨어지지 않은 것으로 봅니다.</li >
51+ <li >4초 시점의 ₩2은 1초간 가격이 떨어지지 않았습니다.</li >
52+ <li >5초 시점의 ₩3은 0초간 가격이 떨어지지 않았습니다.</li >
53+ </ul >
54+
55+ <p >※ 공지 - 2019년 2월 28일 지문이 리뉴얼되었습니다.</p >
56+
57+
58+ > 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ class Status {
4+ private int price ;
5+ private int idx ;
6+
7+ public Status (int price , int idx ){
8+ this .price = price ;
9+ this .idx = idx ;
10+ }
11+
12+ @ Override
13+ public String toString (){
14+ return "[" + this .price + "," + this .idx +"]" ;
15+ }
16+ }
17+
18+ public int [] solution (int [] prices ) {
19+ Stack <Status > stack = new Stack <>();
20+ int len = prices .length ;
21+ int [] answer = new int [len ];
22+
23+ for (int i =0 ; i < len ; i ++){
24+ int price = prices [i ];
25+ Status status = new Status (price ,i );
26+ if (stack .isEmpty ()){
27+ stack .push (status );
28+ } else {
29+ while (true ) {
30+ if (stack .isEmpty ()){
31+ stack .push (status ); // 추가
32+ break ;
33+ }
34+ Status storedStaus = stack .peek ();
35+ int storedPrice = storedStaus .price ;
36+ int storedIdx = storedStaus .idx ;
37+
38+ // 새로운 값이 더 작을 때
39+ if (storedPrice > price ) {
40+ answer [storedIdx ] = i - storedIdx ;
41+ stack .pop (); // 제거
42+ } else {
43+ stack .push (status ); // 추가
44+ break ;
45+ }
46+ }
47+ }
48+ }
49+
50+ // 찌꺼기 처리
51+ while (!stack .isEmpty ()){
52+ Status storedStaus = stack .pop ();
53+ int storedPrice = storedStaus .price ;
54+ int storedIdx = storedStaus .idx ;
55+
56+ answer [storedIdx ] = len - 1 - storedIdx ;
57+ }
58+
59+ return answer ;
60+ }
61+ }
You can’t perform that action at this time.
0 commit comments