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 robot
  • runningCosts[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
🏭 Robot Factory ManagementProduction Line (Consecutive Robots Required)R1Setup: 3Run: 2R2Setup: 6Run: 1R3Setup: 1Run: 3R4Setup: 3Run: 4R5Setup: 4Run: 5Selected WindowLeftRight💰 Cost CalculationTotal Cost = max(Setup Costs) + Window Size × sum(Running Costs)= max(6,1,3) + 3 × (1+3+4)= 6 + 24 = 30Budget: 25 → Need smaller window!Sliding window efficiently finds maximum feasible consecutive robots⚡ Monotonic Deque MagicMaintains maximum setup cost in O(1) timeEach robot enters and leaves deque at most onceResult: O(n) time, O(n) space complexity
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.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.5K Views
Medium Frequency
~25 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen