Maximum Number of Robots Within Budget - Problem
You are the manager of a robotic manufacturing plant and need to optimize the operation of your robots within a limited budget. You have n robots available, each with different charging and operational costs.
Given two arrays:
chargeTimes[i]- the cost to charge the i-th robotrunningCosts[i]- the ongoing operational cost per unit time for the i-th robot
The total cost of running k consecutive robots is calculated as:
Total Cost = max(chargeTimes) + k × sum(runningCosts)
Where max(chargeTimes) is the highest charging cost among the selected robots, and sum(runningCosts) is the sum of all operational costs.
Goal: Find the maximum number of consecutive robots you can operate without exceeding your budget.
Input & Output
example_1.py — Basic Case
$
Input:
chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25
›
Output:
3
💡 Note:
We can run robots [1,2,3] with charge times [6,1,3] and running costs [1,3,4]. Total cost = max(6,1,3) + 3×(1+3+4) = 6 + 24 = 30 > budget. Let's try [0,1,2]: max(3,6,1) + 3×(2+1+3) = 6 + 18 = 24 ≤ 25. We can run 3 consecutive robots.
example_2.py — Small Budget
$
Input:
chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19
›
Output:
0
💡 Note:
Even running a single robot costs at least 11 + 1×10 = 21 > 19, so we cannot run any robots within the budget.
example_3.py — Large Budget
$
Input:
chargeTimes = [1,1,1,1], runningCosts = [1,1,1,1], budget = 50
›
Output:
4
💡 Note:
We can run all robots. Total cost = max(1,1,1,1) + 4×(1+1+1+1) = 1 + 16 = 17 ≤ 50. All 4 consecutive robots can be operated.
Constraints
- 1 ≤ chargeTimes.length == runningCosts.length ≤ 5 × 104
- 1 ≤ chargeTimes[i], runningCosts[i] ≤ 105
- 1 ≤ budget ≤ 1015
- All robots must be consecutive in the array
Visualization
Tap to expand
Understanding the Visualization
1
Setup Cost Analysis
Each machine has a one-time setup cost (charge time) and ongoing operational cost
2
Window Selection
Choose consecutive machines, pay highest setup cost once, plus all operational costs
3
Sliding Window Optimization
Use two pointers to efficiently explore all possible consecutive selections
4
Maximum Tracking
Maintain maximum setup cost using monotonic deque for O(1) updates
Key Takeaway
🎯 Key Insight: Use sliding window with monotonic deque to efficiently track maximum charge cost while exploring all consecutive robot combinations in linear time.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code