728x90
반응형
자료구조 1 Page 노트 정리 1일차 주제 : Stack
백준 25556번 포스택 문제풀이
4개의 스택에서 모든 수를 꺼낼 때 가장 처음에 꺼낸 수가 맨 뒤, 가장 나중에 꺼낸 수가 맨 앞에 위치해야 하는 오름차순으로 정렬되어야 하기 때문에 스택에 원소를 넣을 때 스택이 비어 있거나 이미 들어 있는 원소보다 클 경우에만 원소를 삽입해야 한다.
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
int n = 0;
String input = "";
String[] str = {};
boolean result = true;
Scanner sc = new Scanner(System.in);
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
Stack<Integer> stack3 = new Stack<>();
Stack<Integer> stack4 = new Stack<>();
n = Integer.parseInt(sc.nextLine());
input = sc.nextLine();
str = input.split(" ");
for (int i = 0; i < n; i++) {
if (i == 0) {
stack1.push(Integer.parseInt(str[i]));
} else {
if (stack1.empty() || Integer.parseInt(str[i]) > stack1.peek()) {
stack1.push(Integer.parseInt(str[i]));
} else if (stack2.empty() || Integer.parseInt(str[i]) > stack2.peek()) {
stack2.push(Integer.parseInt(str[i]));
} else if (stack3.empty() || Integer.parseInt(str[i]) > stack3.peek()) {
stack3.push(Integer.parseInt(str[i]));
} else if (stack4.empty() || Integer.parseInt(str[i]) > stack4.peek()) {
stack4.push(Integer.parseInt(str[i]));
} else {
result = false;
}
}
}
if (result) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
728x90
반응형
'📌Zero-base' 카테고리의 다른 글
백준 1021번 회전하는 큐 제로베이스 자료구조 2일차 주체 : Queue (0) | 2024.04.16 |
---|---|
백엔드 커리어 로드맵 - 어떤 백엔드 개발자가 되고 싶은지 (0) | 2024.04.15 |
연소득 과세금액 계산 프로그램 제로베이스 백엔드 스쿨 미니과제 8 (0) | 2024.04.14 |
로또 당첨 프로그램 제로베이스 백엔드 스쿨 미니과제 7 (0) | 2024.04.14 |
가상 선거 및 당선 시뮬레이션 프로그램 제로베이스 백엔드 스쿨 미니과제 6 (0) | 2024.04.13 |