101 Logo
onenoughtone

Problem Statement

Largest Integer Former

You are a marketing manager designing a promotional coupon system for a retail store. Each coupon has a unique code made up of digits. The cost of printing a digit on a coupon depends on its value (0-9), and you have a limited budget for each coupon.

Given an array cost where cost[i] is the cost of printing digit i+1 (costs are 1-indexed), and a target budget target, your task is to form the largest possible integer by printing digits such that the sum of their costs is less than or equal to the target budget.

The integer does not have to start with 1, but it cannot have leading zeros. If it's impossible to form any integer with the given constraints, return an empty string.

Examples

Example 1:

Input: cost = [4, 3, 2, 5, 6, 7, 2, 5, 5], target = 9
Output: "7721"
Explanation: The cost of printing digits 7, 7, 2, 1 is 7 + 7 + 2 + 3 = 19. We can't form a larger integer with the given budget.

Example 2:

Input: cost = [7, 6, 5, 5, 5, 6, 8, 7, 8], target = 12
Output: "85"
Explanation: The cost of printing digits 8, 5 is 8 + 5 = 13. We can't form a larger integer with the given budget.

Example 3:

Input: cost = [2, 4, 6, 2, 4, 6, 4, 4, 4], target = 5
Output: "4"
Explanation: The cost of printing digit 4 is 4. We can't form a larger integer with the given budget.

Example 4:

Input: cost = [6, 10, 15, 40, 40, 40, 40, 40, 40], target = 47
Output: ""
Explanation: We don't have enough budget to print any digit.

Constraints

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

Problem Breakdown

To solve this problem, we need to:

  1. The key insight is to prioritize higher digits (9, 8, 7, ...) to form the largest possible integer.
  2. We can use dynamic programming to determine if it's possible to form an integer with the given budget.
  3. Since we want the largest possible integer, we should try to include as many high-value digits as possible.
  4. We need to handle the case where the budget is insufficient to print any digit.
ProblemSolutionCode
101 Logo
onenoughtone