Fewest Badges for a Row of Scored Players
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Greedy two-pass array problem: assign the minimum total badges so each player gets at least one and any player scoring higher than an adjacent player gets strictly more badges.
Badges for a Row of Players
A line of players has just finished a contest, and each player has an integer score. You must hand out badges to the players following these rules:
- Every player receives at least one badge.
- If a player's score is strictly greater than the score of a player standing directly next to them (on either side), that player must receive strictly more badges than that neighbor.
Players with equal scores place no restriction on each other: they may receive the same number of badges or different numbers.
Write a function that returns the smallest total number of badges that can be handed out while obeying both rules.
If the list of scores is empty, return 0.
Input
scores: a list of integers, wherescores[i]is the score of the player at positioni.
Output
- An integer: the minimum total number of badges.
Constraints
0 <= len(scores) <= 200000 <= scores[i] <= 20000- Only the strict comparisons with immediate left and right neighbors matter.
- An empty list gives
0; a single player gives1.
Examples
Example 1
Input: min_badges([1, 0, 2])
Output: 5
Example 2
Input: min_badges([1, 2, 2])
Output: 4
Hints
Hint 1
Each player has up to two constraints, one from the left neighbor and one from the right. Try handling the two directions separately instead of in one pass.
Hint 2
Start everyone with one badge. Sweep left to right and give a player one more than the left neighbor whenever their score is strictly higher than that neighbor's.
Hint 3
Sweep right to left in the same way, but take the maximum of the current value and the right-derived requirement so the left-side guarantee from the first sweep is not lost. Then sum the array.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every player faces up to two constraints: one relative to the left neighbor and one relative to the right neighbor. Trying to satisfy both at once is awkward, so we handle each direction in its own pass.
- Left-to-right pass. Give everyone one badge. For each position
ifrom 1 onward, ifscores[i] > scores[i-1], setbadges[i] = badges[i-1] + 1. After this pass, all "higher than my left neighbor" requirements hold, using the smallest possible values. - Right-to-left pass. For each position
ifromn-2down to 0, ifscores[i] > scores[i+1], setbadges[i] = max(badges[i], badges[i+1] + 1). Themaxkeeps the left-side guarantee from the first pass intact. - Return the sum of
badges.
Why it is correct
Feasibility. After the second pass, every right-neighbor requirement holds by construction. Left-neighbor requirements still hold: suppose scores[i] > scores[i-1]. The second pass never lowers a value, so badges[i] can only grow. The value badges[i-1] is only raised in the second pass if scores[i-1] > scores[i], which contradicts the assumption. So badges[i] > badges[i-1] still holds.
Optimality. Let L[i] be the length of the strictly increasing run of scores ending at i (reading left to right), and R[i] the length of the strictly increasing run ending at i when reading right to left. Any valid assignment must give player i at least L[i] badges, and at least R[i] badges, because each step up a run needs at least one more badge than the step below it, and the bottom needs at least 1. The first pass computes exactly L[i], and the second yields max(L[i], R[i]). Every player therefore receives their individual lower bound, so the total is the global minimum.
Complexity
- Time: O(n), two linear passes plus a sum.
- Space: O(n) for the badges array.
Reference solution
from typing import List
def min_badges(scores: List[int]) -> int:
n = len(scores)
if n == 0:
return 0
badges = [1] * n
for i in range(1, n):
if scores[i] > scores[i - 1]:
badges[i] = badges[i - 1] + 1
for i in range(n - 2, -1, -1):
if scores[i] > scores[i + 1]:
badges[i] = max(badges[i], badges[i + 1] + 1)
return sum(badges)