|
| 1 | +/** |
| 2 | + * [2410] Maximum Matching of Players With Trainers |
| 3 | + * |
| 4 | + * You are given a 0-indexed integer array players, where players[i] represents the ability of the i^th player. You are also given a 0-indexed integer array trainers, where trainers[j] represents the training capacity of the j^th trainer. |
| 5 | + * The i^th player can match with the j^th trainer if the player's ability is less than or equal to the trainer's training capacity. Additionally, the i^th player can be matched with at most one trainer, and the j^th trainer can be matched with at most one player. |
| 6 | + * Return the maximum number of matchings between players and trainers that satisfy these conditions. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: players = [4,7,9], trainers = [8,2,5,8] |
| 11 | + * Output: 2 |
| 12 | + * Explanation: |
| 13 | + * One of the ways we can form two matchings is as follows: |
| 14 | + * - players[0] can be matched with trainers[0] since 4 <= 8. |
| 15 | + * - players[1] can be matched with trainers[3] since 7 <= 8. |
| 16 | + * It can be proven that 2 is the maximum number of matchings that can be formed. |
| 17 | + * |
| 18 | + * Example 2: |
| 19 | + * |
| 20 | + * Input: players = [1,1,1], trainers = [10] |
| 21 | + * Output: 1 |
| 22 | + * Explanation: |
| 23 | + * The trainer can be matched with any of the 3 players. |
| 24 | + * Each player can only be matched with one trainer, so the maximum answer is 1. |
| 25 | + * |
| 26 | + * |
| 27 | + * Constraints: |
| 28 | + * |
| 29 | + * 1 <= players.length, trainers.length <= 10^5 |
| 30 | + * 1 <= players[i], trainers[j] <= 10^9 |
| 31 | + * |
| 32 | + * |
| 33 | + * Note: This question is the same as <a href="https://leetcode.com/problems/assign-cookies/description/" target="_blank"> 445: Assign Cookies.</a> |
| 34 | + * |
| 35 | + */ |
| 36 | +pub struct Solution {} |
| 37 | + |
| 38 | +// problem: https://leetcode.com/problems/maximum-matching-of-players-with-trainers/ |
| 39 | +// discuss: https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/?currentPage=1&orderBy=most_votes&query= |
| 40 | + |
| 41 | +// submission codes start here |
| 42 | + |
| 43 | +impl Solution { |
| 44 | + pub fn match_players_and_trainers(players: Vec<i32>, trainers: Vec<i32>) -> i32 { |
| 45 | + let mut players = players; |
| 46 | + let mut trainers = trainers; |
| 47 | + players.sort_unstable(); |
| 48 | + trainers.sort_unstable(); |
| 49 | + players |
| 50 | + .into_iter() |
| 51 | + .scan(trainers.into_iter(), |t, p| t.find(|&t| p <= t)) |
| 52 | + .count() as _ |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// submission codes end |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use super::*; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_2410_example_1() { |
| 64 | + let players = vec![4, 7, 9]; |
| 65 | + let trainers = vec![8, 2, 5, 8]; |
| 66 | + |
| 67 | + let result = 2; |
| 68 | + |
| 69 | + assert_eq!( |
| 70 | + Solution::match_players_and_trainers(players, trainers), |
| 71 | + result |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_2410_example_2() { |
| 77 | + let players = vec![1, 1, 1]; |
| 78 | + let trainers = vec![10]; |
| 79 | + |
| 80 | + let result = 1; |
| 81 | + |
| 82 | + assert_eq!( |
| 83 | + Solution::match_players_and_trainers(players, trainers), |
| 84 | + result |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments