Skip to content
BytePatterns

Order After K Digit Passes

MediumSorting#radix-sort#stable-sort#bucket-sort~25m

Problem

A radix sort on whole numbers that are zero or more works one decimal digit at a time, starting from the ones digit. Each pass stably regroups the list by the current digit, 0 first and 9 last, keeping the existing order inside each group. Given a list and a number k, return the list exactly as it looks after the first k passes.

Examples

Input:  nums = [53, 7, 318, 90, 41, 206, 17], k = 1
Output: [90, 41, 53, 206, 7, 17, 318]
Why:    grouped by ones digit: 0, 1, 3, 6, 7, 7, 8; 7 stays ahead of 17
Input:  nums = [53, 7, 318, 90, 41, 206, 17], k = 2
Output: [206, 7, 17, 318, 41, 53, 90]
Why:    the second pass regroups the first pass's output by the tens digit
Input:  nums = [30, 3, 300], k = 0
Output: [30, 3, 300]
Why:    edge case, no passes, so nothing moves

Hints

0 / 3

Stuck on the idea rather than the code? Radix Sort covers it.