Maximum Capacity Within Budget Problem
Description
LeetCode Problem 3814.
You are given two integer arrays costs and capacity, both of length n, where costs[i] represents the purchase cost of the i^th machine and capacity[i] represents its performance capacity.
You are also given an integer budget.
You may select at most two distinct machines such that the total cost of the selected machines is strictly less than budget.
Return the maximum achievable total capacity of the selected machines.
Example 1:
Input: costs = [4,8,5,3], capacity = [1,5,2,7], budget = 8
Output: 8
Explanation:
- Choose two machines with costs[0] = 4 and costs[3] = 3.
- The total cost is 4 + 3 = 7, which is strictly less than budget = 8.
- The maximum total capacity is capacity[0] + capacity[3] = 1 + 7 = 8.
Example 2:
Input: costs = [3,5,7,4], capacity = [2,4,3,6], budget = 7
Output: 6
Explanation:
- Choose one machine with costs[3] = 4.
- The total cost is 4, which is strictly less than budget = 7.
- The maximum total capacity is capacity[3] = 6.
Example 3:
Input: costs = [2,2,2], capacity = [3,5,4], budget = 5
Output: 9
Explanation:
- Choose two machines with costs[1] = 2 and costs[2] = 2.
- The total cost is 2 + 2 = 4, which is strictly less than budget = 5.
- The maximum total capacity is capacity[1] + capacity[2] = 5 + 4 = 9.
Constraints:
1 <= n == costs.length == capacity.length <= 10^51 <= costs[i], capacity[i] <= 10^51 <= budget <= 2 * 10^5
Solution
Approach: Sorting + Two Pointers + Ordered Set
Machines that cost budget or more can never be bought, so drop them and sort the rest by cost into arr. If nothing is left, the answer is 0. Otherwise start with the best single machine, the largest capacity in arr.
For pairs, keep a multiset remain of the machines that can still be paired, initially all of them, and two pointers: i at the cheapest machine and j at the most expensive. For each i, remove arr[i] from remain so it is not paired with itself, then move j left while arr[i].cost + arr[j].cost >= budget, removing each machine that gets dropped. Every machine left in remain can now be bought together with arr[i], so combine arr[i]'s capacity with the largest capacity in remain and update the answer.
The ordered set can be a balanced tree (C++ multiset, Java TreeSet) or, as in the Python and Go tabs, a max-heap whose stale entries are skipped when they reach the top.
Complexity: O(n log n) time and O(n) space, where n is the number of machines.
import heapq
class Solution:
def maxCapacity(self, costs: List[int], capacity: List[int], budget: int) -> int:
arr = []
for a, b in zip(costs, capacity):
if a < budget:
arr.append((a, b))
if not arr:
return 0
arr.sort()
# max-heap of (capacity, index) with lazy deletion
heap = [(-b, i) for i, (_, b) in enumerate(arr)]
heapq.heapify(heap)
alive = [True] * len(arr)
i, j = 0, len(arr) - 1
ans = -heap[0][0]
while i < j:
alive[i] = False
while i < j and arr[i][0] + arr[j][0] >= budget:
alive[j] = False
j -= 1
while heap and not alive[heap[0][1]]:
heapq.heappop(heap)
if heap:
ans = max(ans, arr[i][1] - heap[0][0])
i += 1
return ans