내 풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* @Author: kbs
*/
public class Main {
static int N, M;
static int[] crane, boxes;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
crane = new int[N];
for (int i = 0; i < N; i++) {
crane[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(crane);
M = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
boxes = new int[M];
for (int i = 0; i < M; i++) {
int box = Integer.parseInt(st.nextToken());
boxes[i] = box;
}
Arrays.sort(boxes);
if (boxes[M - 1] > crane[N - 1]) {
System.out.println(-1);
return;
}
Deque<Integer> before = new ArrayDeque<>();
Stack<Integer> after = new Stack<>();
for (int i = M - 1; i >= 0; i--) {
before.addFirst(boxes[i]);
}
int time = 0;
while (true) {
int craneNum = N - 1;
while (!before.isEmpty() && craneNum >= 0) {
int box = before.peekLast();
if (crane[craneNum] >= box) {
before.pollLast();
craneNum--;
} else {
after.add(before.pollLast());
}
}
while (!after.isEmpty()) {
before.addLast(after.pop());
}
time++;
if (before.isEmpty()) break;
}
System.out.println(time);
}
}
참고한 풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* @Author: kbs
*/
public class Main {
static int N, M;
static ArrayList<Integer> crane;
static ArrayList<Integer> boxes;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
crane = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
crane.add(Integer.parseInt(st.nextToken()));
}
M = Integer.parseInt(br.readLine());
boxes = new ArrayList<>();
st = new StringTokenizer(br.readLine());
for (int i = 0; i < M; i++) {
boxes.add(Integer.parseInt(st.nextToken()));
}
crane.sort(Collections.reverseOrder());
boxes.sort(Collections.reverseOrder());
if (boxes.get(0) > crane.get(0)) {
System.out.println(-1);
return;
}
int ans = 0;
while (!boxes.isEmpty()) {
int idx = 0;
for (int i = 0; i < N; ) {
if (idx == boxes.size()) break;
if (crane.get(i) >= boxes.get(idx)) {
boxes.remove(idx);
i++;
} else idx++;
}
ans++;
}
System.out.println(ans);
}
}
결론