231113 숫자 게임

Jongleee·2023년 11월 13일
0

TIL

목록 보기
415/576
public int solution1(int[] a, int[] b) {
	int answer = 0;
	Arrays.sort(a);
	Arrays.sort(b);
	int indexB = a.length - 1;
	for (int i = a.length - 1; i >= 0; i--) {
		if (a[i] < b[indexB]) {
			answer++;
			indexB--;
		}
	}
	return answer;
}

public int solution2(int[] a, int[] b) {
	PriorityQueue<Integer> queueA = new PriorityQueue<>();
	PriorityQueue<Integer> queueB = new PriorityQueue<>();
	int answer = 0;
	for (int i = 0; i < a.length ; i++) {
		queueA.add(a[i]);
		queueB.add(b[i]);
	}
	while (!queueB.isEmpty()&&!queueA.isEmpty()) {
		if (queueA.peek() < queueB.peek()) {
			answer++;
			queueA.poll();
		}
		queueB.poll();
	}
	return answer;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/12987

0개의 댓글