[백준] 1874번 스택수열 - Java
문제 출처
※ 풀이
스택 개념에 대한 활용 문제이다.
스택을 직접 구현해도 되지만 자바 패키지에서 기본적으로 제공해주는 Stack 을 활용하여 풀어보았다.
※ 소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
//input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Stack<Integer> stack = new Stack<>();
int N = Integer.parseInt(br.readLine());
int start = 0;
while (N-- > 0) {
int value = Integer.parseInt(br.readLine());
if (value > start) {
for (int i = start + 1; i <= value; i++) {
stack.push(i);
sb.append('+').append('\n');
}
start = value;
}
else if (stack.peek() != value) {
System.out.println("NO");
return;
}
stack.pop();
sb.append('-').append('\n');
}
System.out.println(sb);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 11727번 2×n 타일링 2 - Java (0) | 2021.03.14 |
---|---|
[백준] 11726번 2×n 타일링 - Java (0) | 2021.03.12 |
[백준] 1259번 팰린드롬수 - Java (0) | 2021.03.10 |
[백준] 14852번 타일 채우기3(시간초과 해결) - Java (0) | 2021.03.10 |
[백준] 2133번 타일 채우기 - Java (0) | 2021.03.10 |