Skip to content

Commit f80e04e

Browse files
committed
[level 2] Title: 주식가격, Time: 34.76 ms, Memory: 73.1 MB -BaekjoonHub
1 parent 3c63f23 commit f80e04e

File tree

2 files changed

+23
-45
lines changed

2 files changed

+23
-45
lines changed

프로그래머스/2/42584. 주식가격/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 72.8 MB, 시간: 49.23 ms
7+
메모리: 73.1 MB, 시간: 34.76 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 09월 02일 13:24:08
19+
2025년 09월 06일 15:18:38
2020

2121
### 문제 설명
2222

프로그래머스/2/42584. 주식가격/주식가격.java

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,37 @@
11
import java.util.*;
2+
23
class Solution {
3-
class Status{
4+
class Stock {
5+
private int time;
46
private int price;
5-
private int idx;
67

7-
public Status(int price, int idx){
8+
public Stock(int price, int time) {
89
this.price = price;
9-
this.idx = idx;
10-
}
11-
12-
@Override
13-
public String toString(){
14-
return "[" + this.price + "," + this.idx +"]";
10+
this.time = time;
1511
}
1612
}
1713

1814
public int[] solution(int[] prices) {
19-
Stack<Status> stack = new Stack<>();
20-
int len = prices.length;
21-
int[] answer = new int[len];
15+
Stack<Stock> stack = new Stack<>();
16+
int[] answer = new int[prices.length];
2217

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-
}
18+
for(int currentTime = 0; currentTime < prices.length; currentTime ++) {
19+
int price = prices[currentTime];
20+
21+
while(!stack.isEmpty()) {
22+
// 가격이 떨어졌다면
23+
if(stack.peek().price > price){
24+
Stock stock = stack.pop();
25+
answer[stock.time] = currentTime - stock.time;
26+
} else break;
4727
}
28+
stack.push(new Stock(price,currentTime));
4829
}
4930

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;
31+
32+
while(!stack.isEmpty()) {
33+
Stock stock = stack.pop();
34+
answer[stock.time] = prices.length - 1 - stock.time;
5735
}
5836

5937
return answer;

0 commit comments

Comments
 (0)