2071. Maximum Number of Tasks You Can Assign Hard

@problem@discussion
#Array#Binary Search#Greedy#Queue#Sorting#Monotonic Queue



1/**
2 * [2071] Maximum Number of Tasks You Can Assign
3 *
4 * You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the i^th task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the j^th worker having workers[j] strength. Each worker can only be assigned to a single task and must have a strength greater than or equal to the task's strength requirement (i.e., workers[j] >= tasks[i]).
5 * Additionally, you have pills magical pills that will increase a worker's strength by strength. You can decide which workers receive the magical pills, however, you may only give each worker at most one magical pill.
6 * Given the 0-indexed integer arrays tasks and workers and the integers pills and strength, return the maximum number of tasks that can be completed.
7 *  
8 * Example 1:
9 * 
10 * Input: tasks = [<u>3</u>,<u>2</u>,<u>1</u>], workers = [<u>0</u>,<u>3</u>,<u>3</u>], pills = 1, strength = 1
11 * Output: 3
12 * Explanation:
13 * We can assign the magical pill and tasks as follows:
14 * - Give the magical pill to worker 0.
15 * - Assign worker 0 to task 2 (0 + 1 >= 1)
16 * - Assign worker 1 to task 1 (3 >= 2)
17 * - Assign worker 2 to task 0 (3 >= 3)
18 * 
19 * Example 2:
20 * 
21 * Input: tasks = [<u>5</u>,4], workers = [<u>0</u>,0,0], pills = 1, strength = 5
22 * Output: 1
23 * Explanation:
24 * We can assign the magical pill and tasks as follows:
25 * - Give the magical pill to worker 0.
26 * - Assign worker 0 to task 0 (0 + 5 >= 5)
27 * 
28 * Example 3:
29 * 
30 * Input: tasks = [<u>10</u>,<u>15</u>,30], workers = [<u>0</u>,<u>10</u>,10,10,10], pills = 3, strength = 10
31 * Output: 2
32 * Explanation:
33 * We can assign the magical pills and tasks as follows:
34 * - Give the magical pill to worker 0 and worker 1.
35 * - Assign worker 0 to task 0 (0 + 10 >= 10)
36 * - Assign worker 1 to task 1 (10 + 10 >= 15)
37 * The last pill is not given because it will not make any worker strong enough for the last task.
38 * 
39 *  
40 * Constraints:
41 * 
42 * 	n == tasks.length
43 * 	m == workers.length
44 * 	1 <= n, m <= 5 * 10^4
45 * 	0 <= pills <= m
46 * 	0 <= tasks[i], workers[j], strength <= 10^9
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/maximum-number-of-tasks-you-can-assign/
52// discuss: https://leetcode.com/problems/maximum-number-of-tasks-you-can-assign/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn max_task_assign(tasks: Vec<i32>, workers: Vec<i32>, pills: i32, strength: i32) -> i32 {
58        0
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_2071() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.