Maximum matching of players with trainers
Question
Analysis
- Sort two arrays
- Two pointer to record matching position
Timing
7 min 41 sec
Code
class Solution {
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
// 2 5 8 8
// 4 7 9
Arrays.sort(players);
Arrays.sort(trainers);
int m = 0;
int n = 0;
int res = 0;
while (m < players.length && n < trainers.length) {
if (players[m] <= trainers[n]) {
res++;
m++;
}
n++;
}
return res;
}
}
Completixy
Time O(nlog(n)) from sorting
Space O(log(n)) from sorting