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)costscost[i]units of paint (0-indexed array) - You must use exactly
targetunits 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
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?"
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code