티스토리 뷰
// 알고리즘
1. 자료구조
2. 입력된 수열을 만들기 위하여 arr배열에 저장
3. 스택을 이용하여 입력 데이터 저장 (1 ~ n까지)
- for문을 돌며 값( i ) 저장
- index를 1부터 시작하여 들어오는 i값과 같으면 poll 다르면 패스
- poll한다면 index 를 1 증가시키고 스택의 가장 위부분(peek)와 비교해서 같으면 출력 다르면 패스
- 반복
더보기
import java.util.*;
import java.io.*;
public class 스택수열_1874 {
static int n, arr[];
static Stack<Integer> st;
static StringBuilder sb;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
arr = new int[n + 1];
st = new Stack<>();
for (int i = 1; i <= n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
// 알고리즘
cal();
System.out.println(st.size() == 0 ? sb.substring(0,sb.length()-1):"NO");
}
private static void cal() {
int idx = 1;
for (int i = 1; i <= n; i++) {
sb.append("+\n");
st.push(i);
while (true) {
if (st.size() > 0 && st.peek() == arr[idx]) {
idx++;
sb.append("-\n");
st.pop();
} else {
break;
}
}
}
}
}
'Algorithm' 카테고리의 다른 글
백준_동전0_11047 (0) | 2021.03.10 |
---|---|
백준_네트워크 복구_2211 (0) | 2021.03.09 |
백준_스타트 링크_5014 (0) | 2021.03.06 |
백준_암호 만들기_1759 (0) | 2021.03.04 |
백준_컨베이어벨트_20055 (0) | 2021.02.27 |