↓Skip to main content
  1. LeetCode/

LeetCode 3814: Maximum Capacity Within Budget

·2 mins· ·
LeetCode Medium Binary-Search Sorting Prefix-Sum
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-18

  • Submissions: Time Limit Exceeded → Accepted

Approach
#

Pick at most two distinct machines whose total cost is strictly less than budget, maximizing total capacity.

One machine is easy: the largest capacity among machines with cost < budget.

For two machines, sort by cost and fix the more expensive one, i. Its partner comes from before i with cost < budget - cost[i], and should have the largest capacity:

  1. Sort by cost and build a prefix maximum of capacity: best[j] = max(cap[0..j]).
  2. For each i, bisect_left(costs, budget - cost[i]) gives the first index idx that is too expensive; valid partners are [0, min(idx, i) - 1].
  3. The candidate is cap[i] + best[min(idx, i) - 1].

Restricting partners to before i keeps the two machines distinct and counts each pair once.

My Attempt: a double loop
#

for i in range(n):
    for j in range(i + 1, n):
        if first_cost + second_cost >= budget:
            break
        max_cap = max(max_cap, first_cap + second_cap)

After sorting, the inner loop can break, but when costs are all low it runs nearly to the end: worst case O(n^2), TLE.

I also tried two pointers with a prefix maximum (left in comments, never submitted) before the bisect version passed.

Solution
#

import bisect

class Solution:
    def maxCapacity(self, costs: List[int], capacity: List[int], budget: int) -> int:
        machines = sorted(zip(costs, capacity))
        sorted_costs = [c for c, _ in machines]

        best_prefix = []
        cur = 0
        for _, cap in machines:
            cur = max(cur, cap)
            best_prefix.append(cur)

        ans = 0
        for i, (cost, cap) in enumerate(machines):
            if cost >= budget:
                break
            ans = max(ans, cap)                                      # one machine
            j = min(bisect.bisect_left(sorted_costs, budget - cost), i) - 1
            if j >= 0:
                ans = max(ans, cap + best_prefix[j])                 # two machines
        return ans

Complexity
#

Time O(n log n), space O(n).

Mistakes to Avoid
#

  • Treating “strictly less” as <=: use bisect_left(costs, budget - cost), which finds the first cost >= budget - cost.
  • Not restricting partners to before i: the same machine could be picked twice.
  • Forgetting the single-machine case.

Interview Explanation
#

I sort machines by cost and build a prefix maximum of capacity. For one machine, take the largest capacity under budget; for two, fix the more expensive machine i, use bisect_left to find the partners costing less than budget - cost[i], restrict them to before i to avoid repeats, and the prefix maximum gives the best partner. Time O(n log n).

Related

LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 2968: Apply Operations to Maximize Frequency Score
·2 mins
LeetCode Hard Sliding-Window Sorting Prefix-Sum Binary-Search Array
LeetCode 2968 note: after sorting, the cheapest common target for a block is its median; prefix sums give the cost in O(1), and it grows with the window, so slide
LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost