1071. Radix Line-up
A mail room tags every parcel with a non-negative integer code, and the codes arrive in the array nums. The sorting machine cannot compare two parcels with each other; all it can do is read one digit position of a code at a time and drop the parcel into one of a fixed number of bins for that digit, keeping parcels in arrival order inside each bin. Repeating this from the least significant digit position to the most significant lines the codes up.
Return a new array with the codes of nums in ascending order, produced by such a stable digit-by-digit (radix) distribution rather than by a comparison sort. With d digit positions and b bins it should run in O(d * (n + b)) time using O(n + b) extra space.
Example 1
- Input:
- nums = [329,17,4081,56,17,900]
- Output:
- [17,17,56,329,900,4081]
- Explanation:
Sorting ascending keeps both copies of 17 together, giving 17, 17, 56, 329, 900, 4081.
Example 2
- Input:
- nums = [305,3,77,3050,12]
- Output:
- [3,12,77,305,3050]
- Explanation:
Ordered by value the codes become 3, 12, 77, 305, 3050.
Constraints
1 ≤ nums.length ≤ 105
0 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms
Expected complexity
- Time
- O(d * (n + b))
- Space
- O(n + b)