예를들어, 첫번째 입력값이 4 -> push,push,push,push,pop
두번째 입력값이 3 -> pop
세번째 입력값이 6 -> push, push, pop
네번째 입력값이 8 -> push, push, pop
다섯번째 입력값이 7 -> pop
여섯번째 입력값이 5 -> pop
이런식으로 작성
+그 다음 값을 찾을 방법 존재하지 않는 경우 : NO출력
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
boolean b = true;
int last = 1;
StringBuilder sb = new StringBuilder();
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
int input = Integer.parseInt(br.readLine());
while (last <= input) {
stack.push(last);
sb.append("+\n");
last++;
}
if (stack.isEmpty()) {
System.out.println(2);
System.out.println("NO");
b = false;
break;
}
int pop = stack.pop();
if (pop == input) {
sb.append("-\n");
}else {
System.out.println("NO");
b = false;
break;
}
}
if (b == true) {
System.out.println(sb);
}
}
}