백준25556: 포스택
문제
https://www.acmicpc.net/problem/25556
문제 정리
- 순열을 오름차순으로 정리하기 위해서는 4개의 스택을 활용해야 한다.
- 스택을 꺼낼 때에는 뒤부터 앞으로 나열한다.
- 즉 스택을 꺼낼 때 오름차순으로 정렬되어 있어야 큰 수를 먼저 꺼내서 오른쪽에 배치할 수 있다.
문제 풀이
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
32
33
34
35
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
ArrayList<Stack<Integer>> sList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
sList.add(new Stack<>());
sList.get(i).push(0);
}
for (int num : list) {
boolean isFlag = sList.stream()
.anyMatch(stack -> {
if (num > stack.peek()) {
stack.push(num);
return true;
}
return false;
});
if (!isFlag) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
댓글남기기