반응형
17298번
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
Stack<Integer> originStack = new Stack<Integer>();
Stack<Integer> maxStack = new Stack<Integer>();
Stack<Integer> NGEStack = new Stack<Integer>();
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
originStack.push(Integer.parseInt(st.nextToken()));
}
while (!originStack.empty()) {
int origin = originStack.peek();
if (maxStack.empty()) {
NGEStack.push(-1);
maxStack.push(originStack.pop());
} else {
if (origin < maxStack.peek()) {
NGEStack.push(maxStack.peek());
maxStack.push(originStack.pop());
} else {
maxStack.pop();
}
}
}
while (!NGEStack.empty()) {
sb.append(NGEStack.pop()).append(" ");
}
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
기존에 스택을 사용하지 안고 이중for문을 사용하여 시간초과 오류가 났다.
반응형
'알고리즘 > 백준 문제[추후 옮길예정]' 카테고리의 다른 글
[JAVA] 백준 10808번 (0) | 2021.05.10 |
---|---|
[JAVA] 백준 17299번 (0) | 2021.05.10 |
[JAVA] 백준 10799번 (0) | 2021.05.08 |
[JAVA] 백준 17413번 (0) | 2021.05.07 |
[JAVA] 백준 1158번 (0) | 2021.05.07 |