Level 1 Counting Sort
The Tally lines up a million monsters, weakest first, without comparing any two. Each has a power level from 1 to k, and k is small.
Write sort_powers(powers: list[int], k: int) -> list[int] that returns a new sorted list (don't change powers).
sort_powers([3, 1, 4, 1, 5, 9, 2, 6, 5, 3], 10) # [1, 1, 2, 3, 3, 4, 5, 5, 6, 9]
sort_powers([2, 2, 1], 2) # [1, 2, 2]
sort_powers([7], 10) # [7]
sort_powers([], 5) # []
len(powers) <= 10^6,1 <= k <= 10^5. Don't usesorted,list.sort,heapqorbisect(the tests check).- ⭐ Bonus: a speed test with a million monsters earns a star in O(n + k).
Show hint
one counter per level, one walk over the army. The counters already describe the sorted army.