Given a list of strings records where each string is in the format Name:Score, find the name of the person with the highest average score.
Each string contains a name and an integer score separated by a colon. A person may appear multiple times with different scores. Return the name of the person with the highest average score.
If there is a tie, return the name that appears first in the list among those tied.
Function Signature
def max_average_name(records: list[str]) -> str:
Rules
-
Each record has exactly one colon. The part before it is the name and the part after it is the score, which may be negative (for example
"Bob:-5"
).
-
Names are compared exactly as written, so
"Alice"
and
"alice"
are different people.
-
A person's average is the sum of all their scores divided by the number of records they have, computed exactly (no rounding).
-
Among people tied for the highest average, return the one whose first record comes earliest in
records
.
Constraints
-
1 <= len(records) <= 10^4
-
Each string is in the format
Name:Score
-
Name
consists of lowercase and uppercase English letters
-
-10^4 <= Score <= 10^4
-
Score
is an integer
Examples
Input: records = ["Alice:90","Bob:80","Alice:100","Bob:70"]
Output: "Alice"
Alice has scores 90 and 100, so her average is 95. Bob has 80 and 70, an average of 75, so Alice has the highest average.
Input: records = ["Alice:50","Bob:60","Charlie:55","Bob:40","Charlie:65"]
Output: "Charlie"
Alice averages 50, Bob averages (60 + 40) / 2 = 50, and Charlie averages (55 + 65) / 2 = 60. Charlie is the only one at the top.
Input: records = ["Zed:-5","Amy:-3","Zed:-1"]
Output: "Zed"
Zed averages (-5 + -1) / 2 = -3 and Amy averages -3, so they tie. Zed's first record comes before Amy's, so the answer is Zed even though Amy sorts first alphabetically.