๋ฐ์ํ
๋ฌธ์
https://www.acmicpc.net/problem/12789
12789๋ฒ: ๋ํค๋ํค ๊ฐ์๋๋ฆฌ๋ฏธ
์ธํ๋ํ๊ต ํ์ํ์์๋ ์ค๊ฐ, ๊ธฐ๋ง๊ณ ์ฌ ๋๋ง๋ค ์ํ ๊ณต๋ถ์ ์ง์น ํ์ฐ๋ค์ ์ํด ๊ฐ์์ ๋๋ ์ฃผ๋ ๊ฐ์ ๋๋ฆฌ๋ฏธ ํ์ฌ๋ฅผ ์ค์ํ๋ค. ์นํ์ด๋ ์ํ ๊ธฐ๊ฐ์ด ๋ ๋๋ง๋ค ๊ฐ์์ ๋ฐ์ ์๊ฐ์ ๋๊ทผ๋
www.acmicpc.net
๊ท์ฌ์ด ๊ฐ์ ๋ฌธ์
๊ณผ์ฐ ์นํ์ด๋ ๊ฐ์์ ๋จน์ ์ ์์๊น...?
ํ์ด
1. stack 2๊ฐ๋ก ํ๊ธฐ
์คํ 2๊ฐ๋ก ํ์๋๋ฐ ํ ํ๋ ์คํ ํ๋๋ก๋ ํ์ด๋ ๋ ๊ฒ ๊ฐ๋ค.
๊ฑฐ๊พธ๋ก ์คํ์ ๋ฃ์๋๋ฐ, ์๊ณ ๋ณด๋ ํ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ฐ๋ก ์ฝ์ ํ๊ณ ์ถ์ถํ ์ ์์๋ค.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static int peekStack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return -1;
} else {
return stack.peek();
}
}
public static void popStack(Stack<Integer> stack) {
if (!stack.isEmpty()) {
stack.pop();
}
}
public static void pushStack(int x, Stack<Integer> stack) {
stack.push(x);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int num = Integer.parseInt(br.readLine());
int start = 1;
int[] arr = new int[num];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < num; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
for (int i = arr.length - 1; i >= 0; i--) {
stack1.add(arr[i]);
}
while (!stack1.isEmpty()) {
if (peekStack(stack1) == start) {
popStack(stack1);
start++;
} else if (peekStack(stack2) == start) {
popStack(stack2);
start++;
} else {
int peekNum = peekStack(stack1);
popStack(stack1);
if (peekNum != -1) {
pushStack(peekNum, stack2);
}
}
}
int length = stack2.size();
for (int i = 0; i < length; i++) {
if (stack2.peek() == start) {
popStack(stack2);
start++;
}
}
if (stack1.isEmpty() && stack2.isEmpty()) {
bw.write("Nice");
} else {
bw.write("Sad");
}
bw.flush();
bw.close();
}
}
2. queue๋ stack์ผ๋ก ํ๊ธฐ
package algorithm;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
public class Algorithm {
public static int peekStack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return -1;
} else {
return stack.peek();
}
}
public static void popStack(Stack<Integer> stack) {
if (!stack.isEmpty()) {
stack.pop();
}
}
public static void pushStack(int x, Stack<Integer> stack) {
stack.push(x);
}
public static int peekQueue(Queue<Integer> queue) {
if(queue.isEmpty()) {
return -1;
} else {
return queue.peek();
}
}
public static void popQueue(Queue<Integer> queue) {
if(!queue.isEmpty()) {
queue.poll();
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int num = Integer.parseInt(br.readLine());
int start = 1;
int[] arr = new int[num];
Queue<Integer> queue = new LinkedList<>();
Stack<Integer> stack2 = new Stack<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < num; i++) {
queue.add(Integer.parseInt(st.nextToken()));
}
while (!queue.isEmpty()) {
if (peekQueue(queue) == start) {
popQueue(queue);
start++;
} else if (peekStack(stack2) == start) {
popStack(stack2);
start++;
} else {
int peekNum = peekQueue(queue);
popQueue(queue);
if (peekNum != -1) {
pushStack(peekNum, stack2);
}
}
}
int length = stack2.size();
for (int i = 0; i < length; i++) {
if (stack2.peek() == start) {
popStack(stack2);
start++;
}
}
if (queue.isEmpty() && stack2.isEmpty()) {
bw.write("Nice");
} else {
bw.write("Sad");
}
bw.flush();
bw.close();
}
}
์ ๋ต
1. queue๋ stack์ด๋ ํ๊ธฐ
2. stack 2๊ฐ๋ก ํ๊ธฐ
๋ฐ์ํ
'๐ฐ๐๐๐๐๐๐๐๐ > ๐ฑ๐๐๐๐๐๐๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/Java] 1๋ก ๋ง๋ค๊ธฐ 1463 (0) | 2023.12.01 |
---|---|
[๋ฐฑ์ค/Java] ์ ๊ณฑ์์ ํฉ 1699 (1) | 2023.11.30 |
[๋ฐฑ์ค/Java] ๋ฐฐ์ด ๋๋ฆฌ๊ธฐ 3 16935 (1) | 2023.11.27 |
[๋ฐฑ์ค/Java] ๋ก๋ด ์๋ฎฌ๋ ์ด์ 2174 (1) | 2023.11.26 |
[๋ฐฑ์ค/Java] ๋ฑ 10866 (0) | 2023.11.22 |