Top k items per category

Write codeHard35 minDatabricks and Snowflake style

Your task

Given events, a list of [category, item, score] triples (category/item are strings, score is an int), return the top k items per category by total score. For each category, sum scores per item, then keep the k items with the highest totals; break ties by item name ascending. Output a list of [category, item, total] sorted by category ascending, then total descending, then item ascending. If a category has fewer than k distinct items, return all of them.

Examples

top_k_per_category(events: list[list], k: int) -> list[list]
Example 1
Inputtop_k_per_category([["food", "pizza", 5], ["food", "sushi", 9], ["food", "pizza", 5], ["toys", "lego", 3]], 1)
Output[["food", "pizza", 10], ["toys", "lego", 3]]