101 Logo
onenoughtone

Code Implementation

Largest Integer Former Implementation

Below is the implementation of the largest integer former:

solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Form the largest integer with digits that add up to target.
* Dynamic Programming approach.
*
* @param {number[]} cost - The cost of printing each digit (1-indexed)
* @param {number} target - The target budget
* @return {string} - The largest possible integer
*/
function largestNumber(cost, target) {
// Initialize DP array
const dp = new Array(target + 1).fill(-1);
dp[0] = 0;
// Fill DP array
for (let i = 1; i <= target; i++) {
for (let j = 1; j <= 9; j++) {
if (i >= cost[j - 1] && dp[i - cost[j - 1]] !== -1) {
dp[i] = Math.max(dp[i], dp[i - cost[j - 1]] + 1);
}
}
}
// Check if it's possible to form any integer
if (dp[target] === -1) {
return "";
}
// Construct the largest possible integer
let result = "";
let remaining = target;
for (let pos = 1; pos <= dp[target]; pos++) {
for (let digit = 9; digit >= 1; digit--) {
if (remaining >= cost[digit - 1] && dp[remaining - cost[digit - 1]] === dp[target] - pos) {
result += digit;
remaining -= cost[digit - 1];
break;
}
}
}
return result;
}
/**
* Form the largest integer with digits that add up to target.
* Greedy approach.
*
* @param {number[]} cost - The cost of printing each digit (1-indexed)
* @param {number} target - The target budget
* @return {string} - The largest possible integer
*/
function largestNumberGreedy(cost, target) {
// Find the digit with the minimum cost
let minCost = Infinity;
let minDigit = -1;
for (let i = 0; i < 9; i++) {
if (cost[i] < minCost) {
minCost = cost[i];
minDigit = i + 1;
}
}
// Check if it's possible to form any integer
if (target < minCost) {
return "";
}
// Construct the largest possible integer
let result = "";
// Try to add as many high digits as possible
for (let digit = 9; digit >= 1; digit--) {
// Calculate how many of this digit we can add
while (target >= cost[digit - 1] && (target - cost[digit - 1]) >= 0) {
result += digit;
target -= cost[digit - 1];
}
}
// If we couldn't form any integer, return an empty string
if (result === "") {
return "";
}
return result;
}
/**
* Form the largest integer with digits that add up to target.
* Recursive approach with memoization.
*
* @param {number[]} cost - The cost of printing each digit (1-indexed)
* @param {number} target - The target budget
* @return {string} - The largest possible integer
*/
function largestNumberRecursive(cost, target) {
// Memoization table
const memo = new Map();
// Recursive function to find the maximum number of digits
function maxDigits(budget) {
// Base cases
if (budget < 0) return -1;
if (budget === 0) return 0;
// Check if we've already computed this
if (memo.has(budget)) {
return memo.get(budget);
}
// Try each digit
let result = -1;
for (let digit = 1; digit <= 9; digit++) {
const subResult = maxDigits(budget - cost[digit - 1]);
if (subResult !== -1) {
result = Math.max(result, subResult + 1);
}
}
// Memoize and return
memo.set(budget, result);
return result;
}
// Check if it's possible to form any integer
const maxLen = maxDigits(target);
if (maxLen === -1) {
return "";
}
// Construct the largest possible integer
let result = "";
let remaining = target;
for (let pos = 1; pos <= maxLen; pos++) {
for (let digit = 9; digit >= 1; digit--) {
if (remaining >= cost[digit - 1] && maxDigits(remaining - cost[digit - 1]) === maxLen - pos) {
result += digit;
remaining -= cost[digit - 1];
break;
}
}
}
return result;
}
// Test cases
console.log(largestNumber([4, 3, 2, 5, 6, 7, 2, 5, 5], 9)); // "7721"
console.log(largestNumber([7, 6, 5, 5, 5, 6, 8, 7, 8], 12)); // "85"
console.log(largestNumber([2, 4, 6, 2, 4, 6, 4, 4, 4], 5)); // "4"
console.log(largestNumber([6, 10, 15, 40, 40, 40, 40, 40, 40], 47)); // ""
console.log(largestNumberGreedy([4, 3, 2, 5, 6, 7, 2, 5, 5], 9)); // "7721"
console.log(largestNumberGreedy([7, 6, 5, 5, 5, 6, 8, 7, 8], 12)); // "85"
console.log(largestNumberGreedy([2, 4, 6, 2, 4, 6, 4, 4, 4], 5)); // "4"
console.log(largestNumberGreedy([6, 10, 15, 40, 40, 40, 40, 40, 40], 47)); // ""
console.log(largestNumberRecursive([4, 3, 2, 5, 6, 7, 2, 5, 5], 9)); // "7721"
console.log(largestNumberRecursive([7, 6, 5, 5, 5, 6, 8, 7, 8], 12)); // "85"
console.log(largestNumberRecursive([2, 4, 6, 2, 4, 6, 4, 4, 4], 5)); // "4"
console.log(largestNumberRecursive([6, 10, 15, 40, 40, 40, 40, 40, 40], 47)); // ""

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that we need to form the largest possible integer by selecting digits such that the sum of their costs is less than or equal to the target budget.
  2. Define DP State: Define a DP array where dp[i] represents the maximum number of digits we can print with a budget of i.
  3. Fill DP Array: Fill the DP array by considering each digit and updating the maximum number of digits we can print with each budget.
  4. Construct the Result: Construct the largest possible integer by greedily selecting the highest possible digit at each step, starting from the most significant digit.
  5. Handle Edge Cases: Handle the case where it's impossible to form any integer with the given budget.
ProblemSolutionCode
101 Logo
onenoughtone