Longest Streak of Growing Daily Distinct Picks
Greedy & IntervalsAuto-graded · 24 test casesLast updated
Find how many days a growing-size distinct-item picking schedule can run, using binary search on the answer plus a capacity feasibility check.
A warehouse holds several bins. Bin j starts with stock[j] items.
Every day you must take items according to a schedule that grows by one each day:
- On day 1 you take one item from exactly 1 bin.
- On day 2 you take one item from each of exactly 2 different bins.
- On day
kyou take one item from each of exactlykdifferent bins.
A bin can be used at most once on a given day, and it can never be taken from once it is empty. Bins may be chosen freely each day, so the same bin can be used on many different days as long as it still has items.
You may stop at any point, but you cannot skip days or do a smaller pick than required. Return the largest D such that days 1, 2, ..., D can all be carried out in order.
If no bins exist, return 0.
Constraints
0 <= len(stock) <= 2 * 10^51 <= stock[j] <= 10^9- The answer is never larger than
len(stock), since daykneedskdifferent bins. - Your solution should run in roughly
O(n log n)time.
Examples
Example 1
Input: max_days_distinct_picks([1, 2, 3])
Output: 3
Example 2
Input: max_days_distinct_picks([2, 2, 2])
Output: 3
Example 3
Input: max_days_distinct_picks([3, 3, 1, 1])
Output: 3
Example 4
Input: max_days_distinct_picks([1, 1, 1, 1, 1])
Output: 2
Hints
Hint 1
If you can finish D days, you can certainly finish D - 1 days. What does that let you do with the search for the answer?
Hint 2
For a fixed D, consider any t of the days. A bin can serve at most min(stock[j], t) of them. Compare that total supply with the demand of the t hardest days (the t largest picks).
Hint 3
The sum of min(stock[j], t) over all bins is quick to compute with a sorted copy and prefix sums, since bins with stock below t contribute their full stock and the rest contribute t. Check every t from 1 to D.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Binary search on the number of days. If D days can be completed, then D - 1 days can too (just ignore the last day). So feasibility is monotone. Also D <= n, because day D needs D different bins. We binary search D in [0, n].
Feasibility test for a fixed D. Think of a 0/1 table with one row per day and one column per bin. A 1 means "bin j is used on day i". Row i must contain exactly i ones, and column j may contain at most stock[j] ones. Using a bin at most once per day is automatic in a 0/1 table.
Such a table exists exactly when, for every t from 1 to D:
(sum of the t largest daily demands) <= sum over bins of min(stock[j], t)
The t largest demands are D, D-1, ..., D-t+1, which sum to t * (2D - t + 1) / 2.
Why it is correct
- Necessary: Take any
tdays. Binjcan serve at most one of them per day and at moststock[j]in total, so at mostmin(stock[j], t). Thetdays with the largest demands are the most demanding set of sizet, so if the inequality fails for them, no schedule exists. - Sufficient: Model it as a flow network: source to day
iwith capacityi, day to bin with capacity 1, bin to sink with capacitystock[j]. A schedule exists iff the max flow equalsD(D+1)/2. For any cut whereSis the set of days on the source side, each bin contributesmin(stock[j], |S|)and the days outsideScontribute their demand. The cheapest cut of a given size picks the largest-demand days, which is exactly the inequality above. So checking alltsuffices.
Comparing total stock with D(D+1)/2 is not enough: for [1, 1, 10] the total is 12 but 3 days is impossible, because at t = 2 the two biggest days need 5 items while only 1 + 1 + 2 = 4 can be supplied.
Computing the supply quickly
Sort the stocks and build prefix sums. If p bins have stock < t, then
sum_j min(stock[j], t) = prefix[p] + t * (n - p)
Iterating t from D down to 1, p only decreases, so a single moving pointer is enough and one feasibility check costs O(n).
Complexity
- Time:
O(n log n)(sorting, plusO(log n)feasibility checks ofO(n)each). - Space:
O(n)for the sorted copy and prefix sums.
Reference solution
from bisect import bisect_left
from typing import List
def max_days_distinct_picks(stock: List[int]) -> int:
a = sorted(stock)
n = len(a)
prefix = [0]
for value in a:
prefix.append(prefix[-1] + value)
def feasible(days: int) -> bool:
if days == 0:
return True
# p = number of bins whose stock is strictly less than t
p = bisect_left(a, days)
for t in range(days, 0, -1):
while p > 0 and a[p - 1] >= t:
p -= 1
supply = prefix[p] + t * (n - p)
demand = t * (2 * days - t + 1) // 2
if supply < demand:
return False
return True
lo, hi = 0, n
while lo < hi:
mid = (lo + hi + 1) // 2
if feasible(mid):
lo = mid
else:
hi = mid - 1
return lo