Skip to the code
+10 XP

#383 Smallest K Bundles

easy

A bundler report lists the sizes of all JavaScript chunks in kilobytes. The "lightest chunks" panel shows the k smallest sizes, smallest first. Equal sizes count separately.

Write smallestK(sizes, k) that returns the k smallest values of sizes in ascending order. k is between 0 and sizes.length. Try to keep only k values in memory while scanning.

Example 1

Input: sizes = [40,12,95,7,33], k = 3
Output: [7,12,33]

The three smallest sizes, smallest first.

Example 2

Input: sizes = [5,1,5,1], k = 3
Output: [1,1,5]

Equal sizes count separately.

Constraints

  • 0 <= k <= sizes.length <= 2 * 10^5
  • 1 <= sizes[i] <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).