Maximum Number of Robots Within Budget
Problem Statement
This one is rated Hard on LeetCode, but it is the same two tools you already know put together.
There are n robots in a row. Robot i has a charge time chargeTimes[i] and a running cost runningCosts[i]. You also have a budget.
If you run a group of k robots, the total cost is:
max(chargeTimes of the group) + k × sum(runningCosts of the group)
The robots you run must be consecutive in the row. Return the largest k you can run without the total cost going over budget. If not even one robot fits, return 0.
Two integer arrays chargeTimes and runningCosts, both of length n, and a 64-bit integer budget.
A single number: the most consecutive robots whose total cost is ≤ budget.
Run the first 3 robots: max(3, 6, 1) + 3 × (2 + 1 + 3) = 6 + 18 = 24 ≤ 25. No group of 4 consecutive robots fits.
Even a single robot costs too much. For example, robot 0 costs 11 + 1 × 10 = 21 > 19.
- • 1 ≤ n ≤ 5 · 10^4
- • 1 ≤ chargeTimes[i], runningCosts[i] ≤ 10^5
- • 1 ≤ budget ≤ 10^15
- • k × sum can reach about 2.5 · 10^14 — use a 64-bit type.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each start, extend the group with a running max and sum; stop when over budget
Variable window; running sum for the costs, a multiset for the max charge
Decreasing deque gives the max charge; a running sum gives the rest of the cost
Fix the first robot. Add robots to the right one at a time, keeping a running max charge and a running sum of running costs. Compute the cost each time. When it goes over budget, stop — more robots only cost more.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.