Problem 366347 · medium · Phase 03 Linear Management & Searching

Bucket Sort into Bins

sorting · bucket sort · floats

Given a list of floats values, each in the half-open range [0, 1), and a positive integer k, distribute the values into k buckets: value v goes into bucket number int(v * k) (computed exactly as Python does). Then sort every bucket in ascending order.

Return the list of k buckets (each a list of floats, possibly empty), bucket 0 first.

Examples

Input:  values = [0.42, 0.11, 0.78, 0.45, 0.05], k = 4
Output: [[0.05, 0.11], [0.42, 0.45], [], [0.78]]
Explanation: Bucket i covers [i/4, (i+1)/4). Nothing falls into [0.5, 0.75).

Constraints

  • 0 <= len(values) <= 5 * 10**4, 1 <= k <= 10**4
  • 0.0 <= values[i] < 1.0
  • Target complexity: O(n log n + k) overall; O(n + k) expected when values are spread evenly.

Goals

  • Distribute values into equal-width buckets in one pass
  • Sort each bucket independently
  • Return the bucket structure itself, including empty buckets
Starting Python…