Skip to content

Commit 398b571

Browse files
committed
Add solution #1449
1 parent ad917fd commit 398b571

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,307 LeetCode solutions in JavaScript
1+
# 1,308 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1106,6 +1106,7 @@
11061106
1446|[Consecutive Characters](./solutions/1446-consecutive-characters.js)|Easy|
11071107
1447|[Simplified Fractions](./solutions/1447-simplified-fractions.js)|Medium|
11081108
1448|[Count Good Nodes in Binary Tree](./solutions/1448-count-good-nodes-in-binary-tree.js)|Medium|
1109+
1449|[Form Largest Integer With Digits That Add up to Target](./solutions/1449-form-largest-integer-with-digits-that-add-up-to-target.js)|Hard|
11091110
1450|[Number of Students Doing Homework at a Given Time](./solutions/1450-number-of-students-doing-homework-at-a-given-time.js)|Easy|
11101111
1451|[Rearrange Words in a Sentence](./solutions/1451-rearrange-words-in-a-sentence.js)|Medium|
11111112
1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](./solutions/1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1449. Form Largest Integer With Digits That Add up to Target
3+
* https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/
4+
* Difficulty: Hard
5+
*
6+
* Given an array of integers cost and an integer target, return the maximum integer you can
7+
* paint under the following rules:
8+
* - The cost of painting a digit (i + 1) is given by cost[i] (0-indexed).
9+
* - The total cost used must be equal to target.
10+
* - The integer does not have 0 digits.
11+
*
12+
* Since the answer may be very large, return it as a string. If there is no way to paint any
13+
* integer given the condition, return "0".
14+
*/
15+
16+
/**
17+
* @param {number[]} cost
18+
* @param {number} target
19+
* @return {string}
20+
*/
21+
var largestNumber = function(cost, target) {
22+
const maxDigits = new Array(target + 1).fill('0');
23+
maxDigits[0] = '';
24+
25+
for (let current = 1; current <= target; current++) {
26+
for (let digit = 1; digit <= 9; digit++) {
27+
const prev = current - cost[digit - 1];
28+
if (prev >= 0 && maxDigits[prev] !== '0') {
29+
const candidate = digit + maxDigits[prev];
30+
if (candidate.length > maxDigits[current].length
31+
|| (candidate.length === maxDigits[current].length && candidate > maxDigits[current])) {
32+
maxDigits[current] = candidate;
33+
}
34+
}
35+
}
36+
}
37+
38+
return maxDigits[target];
39+
};

0 commit comments

Comments
 (0)