Boosting Your Budget With a Limited Number of Ventures
Heaps, Sorting & SearchAuto-graded · 20 test casesLast updated
Greedy selection with a sorted unlock pointer and a max-heap: repeatedly pick the most profitable venture whose entry threshold fits the current funds, up to a fixed number of picks.
A small studio starts with a budget of start_funds. It has a list of n possible ventures it could run, and it is allowed to run at most max_ventures of them, one after another, in any order.
Venture i is described by two values:
entry[i]: the minimum funds the studio must currently hold in order to begin the venture. This is only a threshold. Nothing is deducted when the venture starts.gain[i]: the amount added to the studio's funds as soon as the venture finishes.
Each venture can be run at most once. Because finished ventures increase the funds, ventures that were out of reach earlier may become available later.
Return the largest total funds the studio can hold after running at most max_ventures ventures.
A venture can be started whenever the current funds are greater than or equal to its entry value. If no remaining venture can be started, the studio stops and its funds stay as they are.
Constraints
- $1 \le max_ventures \le 10^5$
- $1 \le n \le 10^5$
- $0 \le start_funds \le 10^9$
- $0 \le gain[i] \le 10^4$
- $0 \le entry[i] \le 10^9$
len(gain) == len(entry) == n- Your solution should run in about $O((n + k)\log n)$ time, where $k$ is
max_ventures. An $O(nk)$ approach is too slow for the largest inputs.
Examples
Example 1
Input: max_funds_after_ventures(2, 0, [1, 2, 3], [0, 1, 1])
Output: 4
Example 2
Input: max_funds_after_ventures(3, 0, [1, 2, 3], [0, 1, 2])
Output: 6
Hints
Hint 1
Funds never go down, because gains are non-negative and entry values are thresholds rather than costs. So a venture that is affordable now stays affordable forever.
Hint 2
At each step, among all ventures you can currently afford, which one leaves you with the most funds? Taking the largest gain never hurts, since more funds only unlock more options.
Hint 3
Sort ventures by entry value and keep a forward-moving pointer to unlock the newly affordable ones. Store unlocked gains in a max-heap (negate values in Python's heapq), and stop early if the heap is empty.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two observations make a greedy strategy work:
- Funds never decrease, since gains are non-negative and entry values are only thresholds. Once a venture becomes affordable, it stays affordable.
- Having more funds is never harmful, because it can only unlock additional ventures.
So at every step, among the ventures that can currently be started, run the one with the largest gain.
To implement this efficiently:
- Sort the ventures by
entry. - Keep a pointer into the sorted list. Before each pick, advance it past every venture whose
entryis at most the current funds, and push each such venture's gain into a max-heap (negated values, sinceheapqis a min-heap). - Pop the largest gain and add it to the funds.
- If the heap is empty, nothing can be started and funds can never change again, so stop.
Why it is correct
Use an exchange argument. Take an optimal sequence that agrees with the greedy choices for the first t picks, so both have the same funds C. Greedy picks p, the affordable venture with the largest gain, while the optimal sequence picks q with gain[p] >= gain[q].
- If
pappears later in the optimal sequence, swappandq.pis affordable at the earlier position. Every venture in between sees funds at least as large as before.qlands inp's old slot, where funds are at leastC, which already coveredq's entry. The final total is unchanged. - If
pnever appears, replaceqwithp. All later funds rise bygain[p] - gain[q] >= 0, so the sequence stays feasible and the final total does not decrease.
Either way an optimal sequence agrees with greedy for one more step, so by induction greedy is optimal. Running extra ventures never lowers funds because gains are non-negative, so "at most" never requires skipping on purpose. The early exit is safe because with an empty heap the funds cannot change, so nothing new can unlock.
Complexity
- Time:
O(n log n + k log n). Sorting costsO(n log n), each venture is pushed once, and there are at mostmin(k, n)pops. - Space:
O(n)for the sorted list and the heap.
Reference solution
import heapq
from typing import List
def max_funds_after_ventures(max_ventures: int, start_funds: int, gain: List[int], entry: List[int]) -> int:
ventures = sorted(zip(entry, gain))
affordable = [] # max-heap of gains (stored negated)
pointer = 0
n = len(ventures)
funds = start_funds
for _ in range(max_ventures):
while pointer < n and ventures[pointer][0] <= funds:
heapq.heappush(affordable, -ventures[pointer][1])
pointer += 1
if not affordable:
break
funds -= heapq.heappop(affordable)
return funds