forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW_154539.java
More file actions
25 lines (21 loc) · 764 Bytes
/
HW_154539.java
File metadata and controls
25 lines (21 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
class HW_154539 {
// 뒷큰수 : 신보다 뒤에 있는 숫자 중에서 자신보다 크면서 가장 가까이 있는 수
// O(N^2) 불가
public int[] solution(int[] numbers) {
int[] answer = new int[numbers.length];
Stack<Integer> stack = new Stack<>();
int size = numbers.length;
stack.push(0);
for(int i=1; i<size; i++){
while(!stack.isEmpty() && numbers[stack.peek()] < numbers[i]){
answer[stack.pop()] = numbers[i];
}
stack.push(i); // numbers[stack.peek()] > numbers[i]
}
while(!stack.empty()){
answer[stack.pop()] = -1;
}
return answer;
}
}