[PS] 배열 합치기 (백준 11728번)
간단한 투포인터 문제이다.
Java
로 풀어보았다.
객체지향
을 녹여내는건 진짜.. 잘하고 싶다!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringBuffer buffer = new StringBuffer(2000000);
static StringTokenizer tokenizer;
static int N, M;
static int[] A, B;
public static void main(String[] args) throws IOException {
input();
combineAndSort();
printOfResult();
}
static void input() throws IOException {
tokenizer = new StringTokenizer(reader.readLine());
N = Integer.parseInt(tokenizer.nextToken());
M = Integer.parseInt(tokenizer.nextToken());
A = new int[N];
B = new int[M];
tokenizer = new StringTokenizer(reader.readLine());
for (int idx = 0; idx < N; idx++) {
A[idx] = Integer.parseInt(tokenizer.nextToken());
}
tokenizer = new StringTokenizer(reader.readLine());
for (int idx = 0; idx < M; idx++) {
B[idx] = Integer.parseInt(tokenizer.nextToken());
}
}
static void combineAndSort() {
int pointerOfA = 0, pointerOfB = 0;
while (pointerOfA < N || pointerOfB < M) {
if (pointerOfB < M &&
(pointerOfA >= N ||
A[pointerOfA] > B[pointerOfB])) {
buffer.append(String.valueOf(B[pointerOfB++]));
}
else {
buffer.append(String.valueOf(A[pointerOfA++]));
}
buffer.append(" ");
}
}
static void printOfResult() {
System.out.println(buffer.toString());
}
}
부족한 점이나 피드백은 사랑입니다 :)