Maximize Resource Harvest with Linear Depletion
Heaps, Sorting & SearchAuto-graded · 25 test casesLast updated
Find the maximum yield from extracting resources over K time steps when each extraction reduces a site's yield by 1 using binary search on the minimum threshold.
You are managing $N$ resource extraction sites. The array a contains non-negative integers representing the initial yield of each site. If you spend a unit of time at site $i$, you collect its current yield $a[i]$, after which its yield decreases by 1 for future units of time. The yield of any site cannot drop below 0 (harvesting a site with 0 yield yields 0 resources and leaves the yield at 0).
You have a total budget of $k$ units of time. In each time unit, you can choose any single site to harvest. Ponds/sites that are not visited keep their current yields unchanged.
Calculate and return the maximum total resources you can harvest across all $k$ time units.
Constraints
- $N = \text{len}(a)$, where $1 \le N \le 10^5$
- $0 \le a[i] \le 10^6$ for each site $i$
- $0 \le k \le 10^9$
Examples
Example 1
Input: max_fish([90, 100], 100)
Output: 7075
Example 2
Input: max_fish([3, 5], 4)
Output: 15
Example 3
Input: max_fish([2], 5)
Output: 3
Hints
Hint 1
Simulating the process step-by-step with a priority queue takes $O(k \log N)$ time, which is too slow when $k$ is up to $10^9$.
Hint 2
Notice that harvesting a site multiple times yields a strictly decreasing arithmetic sequence of values (e.g., $v, v-1, v-2, \dots, 1$).
Hint 3
Can you use binary search to determine a threshold value $T$ such that we harvest all available yields strictly greater than (or equal to) $T$ across all sites?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A naive simulation uses a max-heap to greedily select the highest available yield at each step. While correct, this approach runs in $O(k \log N)$ time, which is infeasible when $k$ is as large as $10^9$.
Instead, observe that harvesting site $i$ yields a sequence of values $a[i], a[i]-1, a[i]-2, \dots, 1$. Because each sequence is strictly decreasing, selecting the $k$ globally largest values across all sites will automatically satisfy the dependency order (you never select $x-1$ before $x$ from the same site).
We can reformulate the problem as finding a threshold value $t$ such that:
- The total number of available harvest yields $\ge t$ is less than or equal to $k$.
- We can greedily take all yields $\ge t$, and then fill any remaining capacity out of $k$ with yields equal to $t - 1$.
We binary search for the smallest integer $t \ge 1$ such that the number of elements $\ge t$ across all sites is $\le k$.
- For a given site with initial yield $x$, the number of yields $\ge t$ is $\max(0, x - t + 1)$.
- The sum of these yields forms an arithmetic progression: $\frac{(x + t) \cdot (x - t + 1)}{2}$.
Once $t$ is identified:
- Sum up all elements $\ge t$ from every site.
- Compute the count of selected items
c. - Fill the remaining $k - c$ operations with the value $t - 1$.
Complexity
- Time Complexity: $O(N \log (\max(a)))$. The binary search takes $O(\log (\max(a)))$ steps. In each step, we iterate through the array of size $N$ to count candidate elements. For $\max(a) \le 10^6$, $\log (\max(a)) \approx 20$, making the operation extremely fast ($O(N)$ overall work).
- Space Complexity: $O(1)$ auxiliary space beyond the input list.
Python 3 Reference Solution
from typing import List
def max_fish(a: List[int], k: int) -> int:
total_available = sum(a)
if k >= total_available:
return sum(x * (x + 1) // 2 for x in a)
lo, hi = 1, max(a) + 1
while lo < hi:
mid = (lo + hi) // 2
count = sum(x - mid + 1 for x in a if x >= mid)
if count <= k:
hi = mid
else:
lo = mid + 1
t = lo
collected_count = 0
total_yield = 0
for x in a:
if x >= t:
cnt = x - t + 1
collected_count += cnt
total_yield += (x + t) * cnt // 2
total_yield += (k - collected_count) * (t - 1)
return total_yield