A팀에서 자연수가 가장 높은 사원부터 나온다고 가정한다. B팀 또한 가장 높은 자연수를 가진 선수부터 출전을 하기로 하지만 만약 출전한 A팀 선수를 이길 수 없다면 점수가 가장 낮은 선수를 대신 내보내서 손해를 최소로 한다. 이 과정에서 B팀이 이긴 횟수를 구한다.
class Solution {
public int solution(int[] A, int[] B) {
// Sort A team in descending order
Queue<Integer> teamA = new PriorityQueue<>(Comparator.reverseOrder()) {{
Arrays.stream(A).forEach(this::add);
}};
// Sort B team in descending order
Queue<Integer> teamB = new PriorityQueue<>(Comparator.reverseOrder()) {{
Arrays.stream(B).forEach(this::add);
}};
// The biggest number player of A team will play first
// If the biggest number player of B team can win then the player plays
// If he can't win then the smallest number player plays (Does nothing in code)
while (!teamA.isEmpty())
if (teamA.poll() < teamB.peek()) teamB.poll();
// The size of teamB is the number of losing of B team
return B.length - teamB.size();
}
}