Budget Growth Through Threshold-Locked Ventures
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Greedy plus min-sorted unlocking and a max-heap: pick up to k ventures, each requiring a minimum budget, to maximize the final budget.
You manage a budget that starts at start_budget. There are n ventures, numbered from 0 to n - 1. Venture i has a minimum budget requirement required[i] and a payout gain[i].
You may carry out ventures one at a time, in any order you choose, and each venture can be done at most once. Venture i can be started only when your current budget is at least required[i]. Starting a venture does not spend any of your budget. When it finishes, your budget grows by gain[i].
You may carry out at most max_ventures ventures (including zero). Return the largest budget you can end up with.
Details
- Payouts are non-negative. Skipping a venture is always allowed, so you are never forced to take one.
- If
max_venturesis0, or no venture is affordable at the start, the answer is simplystart_budget. - If
max_venturesexceeds the number of ventures, you can still do at mostnof them. - Several ventures may share the same requirement or the same payout.
Function
Implement max_final_budget(max_ventures, start_budget, gain, required), where:
max_venturesis the maximum number of ventures allowed,start_budgetis the initial budget,gainis the list of payouts,requiredis the list of minimum budgets, aligned withgainby index.
Return a single integer: the maximum final budget.
Constraints
0 <= max_ventures <= 10^90 <= start_budget <= 10^91 <= n <= 2 * 10^5, wheren = len(gain) = len(required)0 <= gain[i] <= 10^90 <= required[i] <= 10^9- The solution should run in roughly
O(n log n)time.
Examples
Example 1
Input: max_final_budget(2, 0, [1, 2, 3], [0, 1, 1])
Output: 4
Example 2
Input: max_final_budget(3, 0, [1, 2, 3], [0, 1, 2])
Output: 6
Example 3
Input: max_final_budget(3, 5, [1, 2], [10, 20])
Output: 5
Hints
Hint 1
Funds never go down, so once a venture becomes affordable it stays affordable. Think about which affordable venture is always the best one to take next.
Hint 2
Sort ventures by required budget and keep a pointer. After each completion, move the pointer forward to unlock every venture that is now affordable.
Hint 3
Keep the unlocked ventures' payouts in a max-heap. Pop the largest payout each round, and stop early if the heap is empty or you have done max_ventures rounds.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The budget never decreases, because payouts are non-negative and nothing is spent. So once a venture becomes affordable, it stays affordable. This suggests a greedy strategy: in every round, among all ventures currently affordable, do the one with the largest payout.
To implement it efficiently:
- Sort the ventures by their required budget in ascending order.
- Keep a pointer into the sorted list. Before each round, advance the pointer and push the payout of every venture with
required <= budgetinto a max-heap (negate values for Python's min-heapheapq). - Pop the largest payout from the heap and add it to the budget.
- Repeat for at most
min(max_ventures, n)rounds. If the heap is empty at the start of a round, stop, because nothing is affordable and the budget can no longer change.
Why it is correct
Let g be the greedy first choice: the affordable venture with the largest payout. Take any optimal plan.
- If the plan already uses
glater, movegto the front. It is affordable at the start, and doing it earlier only makes the budget larger sooner, so every other step remains feasible and the final budget is unchanged. - If the plan does not use
g, its first ventureowas affordable at the start, sogain[g] >= gain[o]. Replacingowithggives a budget that is at least as large after step one, so every later step stays feasible and the final budget does not decrease.
So some optimal plan starts with g. The remaining problem has the same form, with budget start_budget + gain[g], one fewer allowed venture, and g removed, so induction completes the argument. Because affordable ventures stay affordable, the heap never holds stale entries.
Complexity
- Sorting:
O(n log n). - Each venture is pushed into the heap at most once, and at most
min(max_ventures, n)pops occur, eachO(log n). - Total time:
O(n log n). Space:O(n).
A naive approach that rescans all ventures every round costs O(n * max_ventures), which is far too slow at the upper limits.
Reference solution
from typing import List
import heapq
def max_final_budget(max_ventures: int, start_budget: int, gain: List[int], required: List[int]) -> int:
n = len(gain)
budget = start_budget
# Pair each venture as (required, gain) and sort by requirement.
ventures = sorted(zip(required, gain))
# Max-heap of payouts of affordable ventures (negated for heapq).
unlocked = []
idx = 0
for _ in range(min(max_ventures, n)):
# Unlock every venture that is now affordable.
while idx < n and ventures[idx][0] <= budget:
heapq.heappush(unlocked, -ventures[idx][1])
idx += 1
# Nothing affordable: budget can never grow again.
if not unlocked:
break
# Take the venture with the largest payout.
budget += -heapq.heappop(unlocked)
return budget