You are given two arrays names and scores of equal length n that describe a game in chronological order. In round i, the score of participant names[i] changes by scores[i]. This change can be positive, negative, or zero. Every participant starts with a score of 0. Round numbers use zero-based indexing, matching the array indices.
When all rounds are over, let M be the maximum final total among all participants who appear in names. Only participants whose final total is exactly M are eligible to win.
If more than one participant is eligible, the winner is the eligible participant who first has a running total greater than or equal to M immediately after one of their own updates. A participant's starting balance of 0, before their first update, does not count as reaching any score.
Return the name of the winner.
Function Signature
def cumulative_score_winner(names: list[str], scores: list[int]) -> str:
Rules
-
A participant's running total is checked only right after a round that updates that participant.
-
A running total above
M
counts as reaching
M
, even if the participant later drops back down to
M
.
-
Each round updates exactly one participant, so the earliest qualifying round identifies a single winner.
Constraints
-
1 <= n <= 10^5
-
names.length == scores.length == n
-
1 <= names[i].length <= 20
-
names[i]
consists of lowercase English letters only.
-
-10^4 <= scores[i] <= 10^4
Examples
Input: names = ["mike", "andrew", "mike"], scores = [3, 5, 2]
Output: "andrew"
The final totals are mike = 5 and andrew = 5, so M = 5 and both are eligible. Andrew reaches 5 in round 1, while mike only reaches 5 in round 2.
Input: names = ["andrew", "andrew", "mike"], scores = [3, 2, 5]
Output: "andrew"
The final totals are again andrew = 5 and mike = 5. Andrew reaches 5 in round 1 and mike reaches it in round 2, so andrew wins.
Input: names = ["zed", "amy", "kim", "zed", "amy", "kim"], scores = [10, 2, 8, -9, 3, -3]
Output: "kim"
The final totals are zed = 1, amy = 5 and kim = 5, so M = 5 and zed is not eligible even though he led with 10 after round 0. Kim's total is 8 after round 2, which already counts as reaching 5, while amy first reaches 5 in round 4.