일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- springboot
- 정렬
- select
- 알고리즘
- Database
- mybatis
- 탐욕법
- DFS
- 코테
- join
- IntelliJ
- Effective Java
- SQL
- 이펙티브자바
- 백준
- Spring
- java
- DP
- BFS
- 데이터베이스
- 우선순위큐
- 너비우선탐색
- 깊이우선탐색
- 프로그래머스
- mariaDB
- 다이나믹프로그래밍
- Greedy
- 피보나치
- 그리디알고리즘
- db
Archives
- Today
- Total
땀두 블로그
[백준] 1874번 - 스택 수열 본문


스택을 이용한 문제이다. 스택의 특성상 LIFO특성을 가지기 때문에 입력받은 숫자와 같으면 값을 출력하면서 -를 출력하고 그렇지 않으면 해당 입력값까지 스택에 저장을 한다. 이런 식으로 저장을 하며 인덱스를 올리기 때문에 인덱스보다 낮은 값이 나와 오름차순이 되지 않는 경우를 방지할 수 있다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class p1874 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int a = Integer.parseInt(br.readLine());
int start = 0;
Stack<Integer> st = new Stack<>();
for (int i = 0; i < a; i++) {
int n = Integer.parseInt(br.readLine());
if (start < n) {
for (int j = start+1; j <= n; j++) {
sb.append("+\n");
st.push(j);
}
start = n;
} else if (st.peek() != n) {
System.out.println("NO");
return;
}
sb.append("-\n");
st.pop();
}
System.out.println(sb);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 15596번 - 정수 N개의 합 (0) | 2022.03.20 |
---|---|
[백준] 1676번 - 팩토리얼의 0의 개수 (0) | 2022.03.20 |
[백준] 11866번 - 요세푸스 문제 0 (0) | 2022.03.20 |
[백준] 10866번 - 덱 (0) | 2022.03.20 |
[백준] 10816번 - 숫자 카드 2 (0) | 2022.03.20 |
Comments