Form Largest Integer With Digits That Add up to Target

Given an array of integers cost and an integer target, return the maximum integer you can paint under the following rules:

  • The cost of painting a digit (i + 1) is given by cost[i] (0-indexed).
  • The total cost used must be equal to target.
  • The integer does not have 0 digits.

Since the answer may be very large, return it as a string. If there is no way to paint any integer given the condition, return "0".

Example 1
Inputcost = [4,3,2,5,6,7,2,5,5], target = 9
Output"7772"
The cost to paint the digit '7' is 2 and the digit '2' is 3, so cost("7772") = 2*3 + 3*1 = 9, and "7772" is larger than alternatives such as "977".
Example 2
Inputcost = [7,6,5,5,5,6,8,7,8], target = 12
Output"85"
The cost to paint the digit '8' is 7 and the digit '5' is 5, so cost("85") = 7 + 5 = 12.

Constraints

  • cost.length == 9
  • 1 <= cost[i], target <= 5000

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate