SpaceX is preparing supply kits for its employees before a mission launch. Each item has a name, a priority level (1 being the highest priority), and a weight.
Items must be sorted according to the following rules:
-
Sort by priority level in ascending order (highest priority first).
-
If two items have the same priority, sort by weight in ascending order (lighter items first).
-
If both priority and weight are the same, sort alphabetically by name.
Given a list of items where each item is represented as [name, priority, weight], return the sorted list of item names.
Function Signature
def sort_items(items: list[list]) -> list[str]:
Rules
-
Each item is a list
[name, priority, weight]
, where
name
is a string and
priority
and
weight
are integers.
-
A smaller priority number means a higher priority, so priority 1 comes first.
-
Names compare in plain lexicographic order, and a name that is a prefix of another comes first (
"ro"
before
"rod"
).
-
Two items with the same name, priority and weight produce the same name in the output, so their relative order does not matter.
Constraints
-
1 <= len(items) <= 10^4
-
1 <= priority <= 100
-
1 <= weight <= 1000
-
name
consists of lowercase English letters and has length between 1 and 50.
Examples
Input: items = [["helmet", 1, 500], ["wrench", 2, 300], ["gloves", 1, 200], ["boots", 2, 300], ["visor", 1, 200]]
Output: ["gloves", "visor", "helmet", "boots", "wrench"]
The priority 1 items are gloves (weight 200), visor (weight 200) and helmet (weight 500). gloves and visor tie on weight, so alphabetical order puts gloves first. The priority 2 items boots and wrench both weigh 300, so boots comes before wrench.
Input: items = [["oxygen", 3, 800], ["food", 1, 150], ["water", 1, 200], ["toolkit", 2, 400]]
Output: ["food", "water", "toolkit", "oxygen"]
Sorted by priority the order is food (1, 150), water (1, 200), toolkit (2, 400) and oxygen (3, 800). food and water share priority 1, and food is lighter.
Input: items = [["rope", 2, 100], ["rod", 2, 100], ["ro", 2, 100], ["axe", 10, 5], ["kit", 9, 999]]
Output: ["ro", "rod", "rope", "kit", "axe"]
The three priority 2 items tie on weight, so the names decide: ro is a prefix of the other two, and rod comes before rope because d comes before p. kit (priority 9) comes before axe (priority 10) whatever their weights.