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.

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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(log n) binary search iterations, each taking O(n) time for sliding window check

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for deque in sliding window validation

n
2n
โšก Linearithmic Space

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
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