Skip to content

Commit 60047a7

Browse files
authored
[20251103] PGM / Lv2 / 뒤에 있는 큰 수 찾기 / 이종환
1 parent d9ddee7 commit 60047a7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
class Solution {
6+
public int[] solution(int[] numbers) {
7+
int n = numbers.length;
8+
int[] answer = new int[n];
9+
Stack<Integer> stack = new Stack<>();
10+
11+
for (int i = n - 1; i >= 0; i--) {
12+
// 현재 원소보다 작거나 같은 스택의 원소들을 제거
13+
while (!stack.isEmpty() && stack.peek() <= numbers[i]) {
14+
stack.pop();
15+
}
16+
17+
if (stack.isEmpty()) {
18+
answer[i] = -1;
19+
} else {
20+
// 스택의 맨 위가 뒷 큰수
21+
answer[i] = stack.peek();
22+
}
23+
24+
stack.push(numbers[i]);
25+
}
26+
27+
return answer;
28+
}
29+
}
30+
```

0 commit comments

Comments
 (0)