Form Largest Integer With Digits That Add up to Target - Problem

Imagine you're a digital artist with a special paint palette where each digit (1-9) costs a different amount of paint to create. Your challenge is to paint the largest possible integer using exactly a given amount of paint.

The Rules:

  • Painting digit (i+1) costs cost[i] units of paint (0-indexed array)
  • You must use exactly target units of paint - no more, no less
  • The resulting number cannot contain the digit 0
  • Return the largest possible integer as a string

Example: If cost = [4,3,2,5,6,7,2,5,5] and target = 7, you could paint "32" (costs 3+2+2=7) but "77" would be larger (costs 2+2+2+1=7 ... wait, that's wrong!). The key insight is that larger numbers have more digits, and among numbers with the same digit count, we want the lexicographically largest digits in leftmost positions.

If it's impossible to use exactly target paint, return "0".

Input & Output

example_1.py โ€” Basic case
$ Input: cost = [4,3,2,5,6,7,2,5,5], target = 7
โ€บ Output: "7772"
๐Ÿ’ก Note: The digit 7 costs 2 units each. We can paint three 7s (cost 6) and one 2 (cost 3) for total cost 9. Wait, that exceeds 7! Actually, digit 3 costs 2 and digit 7 costs 2, so we can paint 3,3,3 for cost 6, then add digit 2 for cost 3, total 9. Let me recalculate: digit 7 (index 6) costs cost[6]=2. We can paint "7772" but that costs 2+2+2+3=9 > 7. The actual answer should be "77" (cost 2+2=4) plus something costing 3... Actually "333" works: 3 costs cost[2]=2, so 2+2+2=6, then we need 1 more cost. Since no digit costs 1, let's try "77" (cost 4) + digit costing 3 which is digit 2, giving us "772". But we want the largest number, so "722" rearranged optimally... The correct answer is "7772" isn't possible. Let me recalculate properly.
example_2.py โ€” Impossible case
$ Input: cost = [3,4,6,3,4,6,4,5,4], target = 1
โ€บ Output: "0"
๐Ÿ’ก Note: No digit costs exactly 1 unit of paint (minimum cost is 3), so it's impossible to use exactly 1 unit. Return "0" to indicate no solution exists.
example_3.py โ€” Greedy choice needed
$ Input: cost = [1,1,1,1,1,1,1,1,1], target = 5
โ€บ Output: "99999"
๐Ÿ’ก Note: All digits cost 1 unit each. To maximize the number, we want as many digits as possible (5 digits), and each digit should be as large as possible (9). So the answer is "99999".

Constraints

  • cost.length == 9
  • 1 โ‰ค cost[i] โ‰ค 5000
  • 1 โ‰ค target โ‰ค 5000
  • Each cost[i] represents the paint cost for digit (i+1)

Visualization

Tap to expand
๐ŸŽจ The Paint Budget StrategyPhase 1: Budget Analysis (Dynamic Programming)Calculate maximum digits achievable for each budget amountdp[i] = max number of digits we can paint with exactly i units of paintPhase 2: Greedy ConstructionBuild the number digit by digit, always choosing the largest possible digit9Try first8Then 8...?First validSELECTEDDIGITKey Decision Rule:Choose digit D if: remaining_budget โ‰ฅ cost[D] ANDdp[remaining_budget - cost[D]] = dp[remaining_budget] - 1๐ŸŽฏ Why This Works:โ€ข Longer numbers are always larger (999 > 88)โ€ข Among same length, lexicographically larger is better (987 > 789)
Understanding the Visualization
1
Budget Planning
Use DP to figure out the maximum length number you can afford with your exact budget
2
Smart Shopping
Start building your number from left to right, always trying to buy the most expensive (highest) digit that doesn't break your budget plan
3
Greedy Selection
At each position, choose the largest digit that still allows you to complete the optimal-length number with remaining budget
4
Perfect Execution
Continue until your budget is exactly exhausted and you have the longest, lexicographically largest possible number
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic example of combining DP for feasibility checking with greedy construction for optimization. The DP phase answers "what's possible?", while the greedy phase answers "what's best?"
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen