Skip to the code
cf.
+10 XP

#510 Preload Assets Within a Byte Budget

easy

Before the first paint, a page may preload some of its assets. Asset i takes sizes[i] kilobytes and has a usefulness score values[i]. The total size of preloaded assets must not exceed budget kilobytes, and each asset is preloaded at most once.

Write bestPreload(sizes, values, budget) that returns the largest total usefulness you can preload.

Example 1

Input: sizes = [1,3,4], values = [15,20,30], budget = 4
Output: 35

Assets 0 and 1 take exactly 4 KB and are worth 15 + 20.

Example 2

Input: sizes = [5], values = [10], budget = 4
Output: 0

The only asset doesn't fit.

Constraints

  • 1 <= sizes.length <= 100
  • sizes.length === values.length
  • 1 <= sizes[i] <= 1000
  • 0 <= values[i] <= 1000
  • 0 <= budget <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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