Skip to the code
+50 XP

#522 Biggest Number for a Pixel Budget

hard

A tiny LED display draws digits with lit pixels. Drawing digit d (1 to 9) uses costs[d - 1] pixels, and the digit 0 can't be drawn. You must light exactly budget pixels, and any digit may be drawn many times.

Write biggestNumber(costs, budget) that returns, as a string, the largest number you can draw using exactly budget pixels. If it can't be done, return "0".

Example 1

Input: costs = [5,4,3,6,7,8,3,6,6], budget = 10
Output: "772"

At most three digits fit. Two 7s (3 pixels each) and a 2 (4 pixels) use exactly 10, and 772 is the largest order.

Example 2

Input: costs = [2,2,2,2,2,2,2,2,2], budget = 5
Output: "0"

Every digit costs 2, so an odd budget can't be spent exactly.

Constraints

  • costs.length === 9
  • 1 <= costs[i] <= 5000
  • 1 <= budget <= 100000
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).