Largest Threshold Met by Daily Step Counts
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Find the largest integer k such that at least k entries in a list are at least k, testing sorting or counting-based selection and reasoning about monotone thresholds.
Largest Threshold Met by Daily Step Counts
A walker records how many kilometres they covered on each day of a long trip. You are given these records as a list of positive integers, daily_km, where daily_km[i] is the distance for day i. The days are in no particular order.
Define a milestone value m as follows: m is valid if at least m of the days have a distance of at least m.
Your task is to return the largest valid milestone value.
Details
- A day counts toward
mwhenever its distance is greater than or equal tom; equal values are counted. - The same day may count toward the check for many different values of
m. - The result can never exceed the number of days, since you cannot have more than
nqualifying days. - Do not rely on the input being sorted. Avoid assumptions that break with duplicate values.
Function
Implement a function that takes the list daily_km and returns a single integer: the largest valid milestone value.
Constraints
1 <= len(daily_km) <= 2 * 10^51 <= daily_km[i] <= 10^9- Aim for a solution that runs comfortably within time limits for the largest input size.
Examples
Example 1
Input: largest_milestone([1, 3, 3, 5, 6])
Output: 3
Example 2
Input: largest_milestone([10, 8, 5, 4, 3])
Output: 4
Example 3
Input: largest_milestone([1, 1, 1])
Output: 1
Hints
Hint 1
Think of the days sorted from longest to shortest. If you look at the k-th longest day, what does its value tell you about whether k qualifying days exist?
Hint 2
At least k days are >= k exactly when the k-th largest value is >= k. Once this fails for some k, it fails for every larger k, so you can stop early.
Hint 3
For a linear-time variant: the answer is at most n, so clamp each value to n, count occurrences in buckets, and sweep k from n down to 1 with a running total of days with value >= k.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort the distances from largest to smallest. Let the sorted list be d[0] >= d[1] >= ... >= d[n-1], and walk through it using a 1-based position i.
For any k between 1 and n, there are at least k days with distance >= k if and only if d[k-1] >= k, that is, the k-th largest value is at least k.
Scan i = 1, 2, .... While d[i-1] >= i, record i as the current best. At the first position where the condition fails, stop. The last recorded value is the answer. If nothing succeeds, the answer is 0, though under the given constraints it is always at least 1.
Why it is correct
- If
d[k-1] >= k, the firstksorted entries are all at leastd[k-1], hence at leastk, sokqualifying days exist. - If
d[k-1] < k, any group ofkdays must include an entry at sorted positionk-1or later, and all of those are belowk. So fewer thankdays reachk. - The condition is monotone: if it fails at
k, then for any largerk'we haved[k'-1] <= d[k-1] < k < k', so it fails too. The feasible values therefore form a prefix1..E, and stopping at the first failure is safe. Ecannot exceedn, which the loop respects naturally.
Complexity
- Time:
O(n log n)for sorting, plusO(n)for the scan. - Space:
O(n)for the sorted copy (the input is not modified).
Linear-time variant
Since the answer is at most n, clamp each value to min(value, n), count occurrences in an array of size n + 1, then sweep k from n down to 1 while accumulating how many days have value >= k. The first k where the running total is >= k is the answer. This is O(n) time and space.
Reference solution
from typing import List
def largest_milestone(daily_km: List[int]) -> int:
ordered = sorted(daily_km, reverse=True)
answer = 0
for i, distance in enumerate(ordered, 1):
if distance >= i:
answer = i
else:
break
return answer