You are given a list of employees, where each employee is represented by their name, city, and salary. Group the employees by their city and, for each city, find the highest salary earned by any employee in that city.
Return a dictionary where each key is a city name and the corresponding value is the highest salary among all employees in that city.
Function Signature
def highest_salary_by_city(employees: list[dict]) -> dict[str, int]:
Rules
-
Each employee is a dictionary with the keys
"name"
(string),
"city"
(string) and
"salary"
(integer).
-
Two employees are in the same city when their
city
strings are equal.
-
The result has exactly one key for each distinct city in the input. Key order does not matter; the result is compared as a dictionary.
Constraints
-
1 <= len(employees) <= 10^5
-
1 <= len(name), len(city) <= 50
-
0 <= salary <= 10^9
Examples
Input: employees = [{"name": "Alice", "city": "NYC", "salary": 90000},
{"name": "Bob", "city": "NYC", "salary": 120000},
{"name": "Charlie", "city": "LA", "salary": 85000},
{"name": "Dave", "city": "LA", "salary": 95000},
{"name": "Eve", "city": "SF", "salary": 150000}]
Output: {"NYC": 120000, "LA": 95000, "SF": 150000}
NYC has Alice (90000) and Bob (120000), so its maximum is 120000. LA has Charlie (85000) and Dave (95000), so its maximum is 95000. SF has only Eve (150000).
Input: employees = [{"name": "John", "city": "Boston", "salary": 70000}]
Output: {"Boston": 70000}
There is only one employee, so Boston's highest salary is 70000.
Input: employees = [{"name": "Ann", "city": "Austin", "salary": 0},
{"name": "Raj", "city": "Austin", "salary": 0},
{"name": "Li", "city": "Denver", "salary": 60000},
{"name": "Mo", "city": "Denver", "salary": 60000}]
Output: {"Austin": 0, "Denver": 60000}
Both Austin employees earn 0, so Austin's highest salary is 0. Both Denver employees earn 60000, and a tie still gives a single value for the city.