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
Maximum Number of Robots Within Budget INPUT Robots (indices 0-4): 3 6 1 3 4 chargeTimes[] 2 1 3 4 5 runningCosts[] Budget Limit 25 Cost Formula: max(charge) + k*sum(run) ALGORITHM STEPS 1 Sliding Window Use two pointers L, R 2 Monotonic Deque Track max chargeTime 3 Calculate Cost Check if within budget 4 Shrink Window Move L if over budget Window [2,3,4]: indices 2-4 3 6 1 3 4 max=4, sum=3+4+5=12 cost=4+3*12=40 > 25 Over budget! Try k=3 FINAL RESULT Optimal Window Found: Robots at indices [0, 1, 2] charge=3 run=2 charge=6 run=1 charge=1 run=3 Cost Calculation: max(charge) = max(3,6,1) = 6 sum(run) = 2+1+3 = 6 Total = 6 + 3*6 = 6 + 18 = 24 24 <= 25 [OK] Maximum Robots 3 3 consecutive robots within budget of 25 Key Insight: Use a sliding window with a monotonic deque to efficiently track the maximum charge time. The deque maintains indices in decreasing order of charge times, giving O(1) max lookup. Shrink window from left when cost exceeds budget. Overall time complexity: O(n). TutorialsPoint - Maximum Number of Robots Within Budget | Optimal Solution
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