2398. Maximum Number of Robots Within Budget Hard

@problem@discussion
#Array#Binary Search#Queue#Sliding Window#Heap (Priority Queue)#Prefix Sum



1/**
2 * [2398] Maximum Number of Robots Within Budget
3 *
4 * You have n robots. You are given two 0-indexed integer arrays, chargeTimes and runningCosts, both of length n. The i^th robot costs chargeTimes[i] units to charge and costs runningCosts[i] units to run. You are also given an integer budget.
5 * The total cost of running k chosen robots is equal to max(chargeTimes) + k * sum(runningCosts), where max(chargeTimes) is the largest charge cost among the k robots and sum(runningCosts) is the sum of running costs among the k robots.
6 * Return the maximum number of consecutive robots you can run such that the total cost does not exceed budget.
7 *  
8 * Example 1:
9 * 
10 * Input: chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25
11 * Output: 3
12 * Explanation: 
13 * It is possible to run all individual and consecutive pairs of robots within budget.
14 * To obtain answer 3, consider the first 3 robots. The total cost will be max(3,6,1) + 3 * sum(2,1,3) = 6 + 3 * 6 = 24 which is less than 25.
15 * It can be shown that it is not possible to run more than 3 consecutive robots within budget, so we return 3.
16 * 
17 * Example 2:
18 * 
19 * Input: chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19
20 * Output: 0
21 * Explanation: No robot can be run that does not exceed the budget, so we return 0.
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	chargeTimes.length == runningCosts.length == n
27 * 	1 <= n <= 5 * 10^4
28 * 	1 <= chargeTimes[i], runningCosts[i] <= 10^5
29 * 	1 <= budget <= 10^15
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/maximum-number-of-robots-within-budget/
35// discuss: https://leetcode.com/problems/maximum-number-of-robots-within-budget/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40    pub fn maximum_robots(charge_times: Vec<i32>, running_costs: Vec<i32>, budget: i64) -> i32 {
41        0
42    }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_2398() {
53    }
54}
55


Back
© 2025 bowen.ge All Rights Reserved.