The Maximum Number of Robots Within Budget coding problem gives you two arrays: chargeTimes (charge time for each robot) and runningCosts (running cost for each robot). You are also given a budget. You want to select a contiguous subset of robots such that the total cost (sum of running costs + maximum charge time within the subset) does not exceed the budget. Find the maximum number of robots you can select.
InMobi and Amazon frequently ask this hard problem because it combines sliding window, monotonic queue (or segment tree/sparse table) for range maximum queries, and binary search on the answer. The problem requires carefully optimizing the window calculation and the monotonic property of the answer.
Binary Search on Answer + Sliding Window with Monotonic Queue:
k) is a monotonic property: if you can select k robots within budget, you can also select any k' < k robots. This allows binary search on the length k of the contiguous subset. The search range is [0, N].check(k) function: For a given length k, we need to determine if there exists any contiguous subset of k robots that satisfies the budget constraint.
k.runningCosts within the current window. This can be updated in O(1) as the window slides.chargeTime within the current window. This is the tricky part. A max_heap could work (O(log k) per operation), but a more efficient monotonic decreasing deque (double-ended queue) allows O(1) amortized time for range maximum queries.runningCosts sum. Add chargeTime to monotonic deque (remove smaller elements from back, then add to back).runningCosts sum. Remove chargeTime from front of deque if it's the maximum.current_running_costs_sum + current_max_charge_time * k <= budget. If true, check(k) returns true.k satisfies the budget, return false.Time Complexity: O(N * log N) (binary search log N iterations, each check(k) is O(N) with sliding window and monotonic deque).
chargeTimes = [3, 6, 1, 3, 4], runningCosts = [2, 1, 3, 4, 5], budget = 25.
Binary search for k in [0, 5].
Try check(k=3):
Window 1: robots [3,6,1] from chargeTimes, [2,1,3] from runningCosts. (Indices 0,1,2)
chargeTime in window (3,6,1) = 6.runningCosts in window (2,1,3) = 2+1+3 = 6.6 + 6 * 3 = 6 + 18 = 24.24 <= 25. Yes! check(3) returns true.Since check(3) is true, binary search will try for a larger k. (e.g., k=4).
Window: k=4. Try chargeTimes=[3,6,1,3], runningCosts=[2,1,3,4].
chargeTime = 6.runningCosts = 2+1+3+4 = 10.10 + 6 * 4 = 10 + 24 = 34.34 > 25. No.
Slide window: chargeTimes=[6,1,3,4], runningCosts=[1,3,4,5].chargeTime = 6.runningCosts = 1+3+4+5 = 13.13 + 6 * 4 = 13 + 24 = 37.37 > 25. No.Binary search will converge to 3.
max_chargeTime makes check(k) O(N*k) or O(N log k), leading to O(N^2 log N) or O(N log^2 N) overall. The monotonic deque is crucial for O(N).left and right pointers and updating sums/deque.max_chargeTime * k can be large; use 64-bit integers if necessary.For the Array Monotonic Queue Sliding Window Queue Binary Search Heap (Priority Queue) Prefix Sum interview pattern, this problem is a benchmark for combining binary search on the answer with efficient window queries. Mastering the monotonic deque for range maximum/minimum is a must for such problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Shortest Subarray with Sum at Least K | Hard | Solve | |
| Sliding Window Maximum | Hard | Solve | |
| Max Value of Equation | Hard | Solve | |
| Maximize the Minimum Powered City | Hard | Solve | |
| Constrained Subsequence Sum | Hard | Solve |