Skip to content

Commit 548d4f9

Browse files
committed
Add solution #1416
1 parent f1457ea commit 548d4f9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,291 LeetCode solutions in JavaScript
1+
# 1,292 LeetCode solutions in JavaScript
22

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

@@ -1082,6 +1082,7 @@
10821082
1413|[Minimum Value to Get Positive Step by Step Sum](./solutions/1413-minimum-value-to-get-positive-step-by-step-sum.js)|Easy|
10831083
1414|[Find the Minimum Number of Fibonacci Numbers Whose Sum Is K](./solutions/1414-find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k.js)|Medium|
10841084
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
1085+
1416|[Restore The Array](./solutions/1416-restore-the-array.js)|Hard|
10851086
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
10861087
1431|[Kids With the Greatest Number of Candies](./solutions/1431-kids-with-the-greatest-number-of-candies.js)|Easy|
10871088
1436|[Destination City](./solutions/1436-destination-city.js)|Easy|

solutions/1416-restore-the-array.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1416. Restore The Array
3+
* https://leetcode.com/problems/restore-the-array/
4+
* Difficulty: Hard
5+
*
6+
* A program was supposed to print an array of integers. The program forgot to print whitespaces
7+
* and the array is printed as a string of digits s and all we know is that all integers in the
8+
* array were in the range [1, k] and there are no leading zeros in the array.
9+
*
10+
* Given the string s and the integer k, return the number of the possible arrays that can be
11+
* printed as s using the mentioned program. Since the answer may be very large, return it modulo
12+
* 109 + 7.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @param {number} k
18+
* @return {number}
19+
*/
20+
function numberOfArrays(s, k) {
21+
const MOD = 1e9 + 7;
22+
const dp = new Array(s.length + 1).fill(0);
23+
dp[0] = 1;
24+
25+
for (let i = 1; i <= s.length; i++) {
26+
let num = 0;
27+
for (let j = i - 1; j >= Math.max(0, i - String(k).length); j--) {
28+
if (j < 0) break;
29+
num = Number(s[j]) * Math.pow(10, i - j - 1) + num;
30+
if (num <= k && (s[j] !== '0')) {
31+
dp[i] = (dp[i] + dp[j]) % MOD;
32+
}
33+
}
34+
}
35+
36+
return dp[s.length];
37+
}

0 commit comments

Comments
 (0)