Skip to content

Commit e98b853

Browse files
committed
[Stack] programmers 주식가격
1 parent b205276 commit e98b853

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
| 05 | ⭐️ | [Baekjoon-2867 수열의 값](./src/Stack/P2867) | |
204204
| 06 | | [SWEA-1218 괄호 짝짓기](./src/Stack/swea1218) | |
205205
| 07 | | [SWEA-1223 계산기2](./src/Stack/swea1223) | |
206+
| 08 | | [Programmers 주식가격](./src/Stack/prg42584) | |
206207

207208
### Queue
208209

src/Stack/prg42584/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [programmers - 스택/큐] 주식가격
2+
3+
![image](https://user-images.githubusercontent.com/22045163/115883783-2600e600-a489-11eb-86ed-42fb02091879.png)

src/Stack/prg42584/Solution.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Stack.prg42584;
2+
3+
import java.util.*;
4+
5+
class Solution {
6+
public int[] solution(int[] prices) {
7+
8+
int N = prices.length;
9+
int[] res = new int[N];
10+
Stack<Price> s = new Stack<>();
11+
12+
for (int i = 0; i < N; i++) {
13+
int cur = i == N-1 ? 0 : prices[i];
14+
while (!s.isEmpty() && s.peek().price > cur) {
15+
Price p = s.pop();
16+
res[p.time] = i - p.time;
17+
}
18+
s.push(new Price(cur, i));
19+
}
20+
21+
return res;
22+
}
23+
}
24+
25+
class Price {
26+
int price, time;
27+
28+
public Price(int price, int time) {
29+
this.price = price;
30+
this.time = time;
31+
}
32+
}

0 commit comments

Comments
 (0)