Maximum Score Level Backed by Enough Servers
Arrays & HashingAuto-graded · 21 test casesLast updated
Find the largest k such that at least k scores are k or higher, using counting with capped values to reach linear time.
You are given a list of integer scores, one per server in a fleet. Find the highest level k that the fleet can support.
A level k is supported if at least k servers have a score that is greater than or equal to k. A score exactly equal to k counts toward that level.
Return the largest supported level. If no positive level is supported, return 0.
Rules
- The level
kcan never be larger than the number of servers. - Scores may be zero or negative. Such scores never count toward any positive level.
- An empty list has answer
0.
Input and Output
- Input:
scores, a list of integers. - Output: a single integer, the largest supported level, or
0if there is none.
Constraints
0 <= len(scores) <= 10^5- Each score is an integer that fits in a 32-bit signed integer (it may be negative, zero, or very large).
- A solution that runs in O(n) time is achievable and preferred.
Examples
Example 1
Input: max_supported_level([3, 0, 6, 1, 5])
Output: 3
Example 2
Input: max_supported_level([0, 0])
Output: 0
Hints
Hint 1
The answer is always between 0 and n, where n is the number of servers, so you only need to consider those n + 1 levels.
Hint 2
For the purpose of this problem, a score larger than n behaves exactly like a score equal to n. Cap large scores, and ignore negative ones.
Hint 3
Count how many scores land on each value from 0 to n, then walk k downward from n while keeping a running total of scores that are at least k. The first k where the total reaches k is the answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Let f(k) be the number of scores that are >= k. We want the largest k with f(k) >= k.
Two observations narrow the search:
kcannot exceedn, the number of servers, becausef(k) <= n. So any score abovenbehaves the same as a score of exactlyn, and we can cap scores atn.- Negative scores never help, since we only test
k >= 1and a negative score is never>= 1. We simply skip them (this also avoids Python's negative indexing wrapping around).
After capping, every useful score lies in [0, n]. We tally them in an array counts of size n + 1. Then we scan k from n down to 1, keeping a running sum of counts[k] + ... + counts[n], which equals f(k). The first k where this sum is at least k is the answer. If the loop finishes with no success, return 0.
Why it is correct
- At each step of the downward scan, the running sum equals exactly the number of scores that are
>= k: capped scores are added at bucketn(so they are counted at everyk <= n), and negative scores are never added. - We test candidates from largest to smallest and return at the first success, so no larger qualifying
kexists. k = 0always holds trivially, so returning0as the fallback is correct. This also covers the empty list, where the loop does not run.
Complexity
- Time: O(n), one pass to fill the buckets and at most n steps in the scan.
- Space: O(n) for the buckets.
An alternative is to sort descending and find the last index i where sorted[i] >= i + 1, giving O(n log n) time.
Reference solution
from typing import List
def max_supported_level(scores: List[int]) -> int:
n = len(scores)
counts = [0] * (n + 1)
for s in scores:
if s >= n:
counts[n] += 1
elif s >= 0:
counts[s] += 1
at_least = 0
for k in range(n, 0, -1):
at_least += counts[k]
if at_least >= k:
return k
return 0