Skip to content

Commit 90426f2

Browse files
committed
Add solution #1420
1 parent 8c9073c commit 90426f2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-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,295 LeetCode solutions in JavaScript
1+
# 1,296 LeetCode solutions in JavaScript
22

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

@@ -1086,6 +1086,7 @@
10861086
1417|[Reformat The String](./solutions/1417-reformat-the-string.js)|Easy|
10871087
1418|[Display Table of Food Orders in a Restaurant](./solutions/1418-display-table-of-food-orders-in-a-restaurant.js)|Medium|
10881088
1419|[Minimum Number of Frogs Croaking](./solutions/1419-minimum-number-of-frogs-croaking.js)|Medium|
1089+
1420|[Build Array Where You Can Find The Maximum Exactly K Comparisons](./solutions/1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js)|Hard|
10891090
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
10901091
1431|[Kids With the Greatest Number of Candies](./solutions/1431-kids-with-the-greatest-number-of-candies.js)|Easy|
10911092
1436|[Destination City](./solutions/1436-destination-city.js)|Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons
3+
* https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/
4+
* Difficulty: Hard
5+
*
6+
* You are given three integers n, m and k. Consider the following algorithm to find the maximum
7+
* element of an array of positive integers.
8+
*
9+
* You should build the array arr which has the following properties:
10+
* - arr has exactly n integers.
11+
* - 1 <= arr[i] <= m where (0 <= i < n).
12+
* - After applying the mentioned algorithm to arr, the value search_cost is equal to k.
13+
*
14+
* Return the number of ways to build the array arr under the mentioned conditions. As the answer
15+
* may grow large, the answer must be computed modulo 109 + 7.
16+
*/
17+
18+
/**
19+
* @param {number} n
20+
* @param {number} m
21+
* @param {number} k
22+
* @return {number}
23+
*/
24+
function numOfArrays(length, maxValue, searchCost) {
25+
const MOD = 1e9 + 7;
26+
const cache = Array.from({ length: length + 1 }, () =>
27+
Array.from({ length: maxValue + 1 }, () =>
28+
Array(searchCost + 1).fill(-1)
29+
)
30+
);
31+
32+
function computeArrays(pos, currentMax, remainingCost) {
33+
if (pos === length) return remainingCost === 0 ? 1 : 0;
34+
if (remainingCost < 0) return 0;
35+
if (cache[pos][currentMax][remainingCost] !== -1) {
36+
return cache[pos][currentMax][remainingCost];
37+
}
38+
39+
let total = 0;
40+
for (let value = 1; value <= currentMax; value++) {
41+
total = (total + computeArrays(pos + 1, currentMax, remainingCost)) % MOD;
42+
}
43+
44+
if (remainingCost > 0) {
45+
for (let value = currentMax + 1; value <= maxValue; value++) {
46+
total = (total + computeArrays(pos + 1, value, remainingCost - 1)) % MOD;
47+
}
48+
}
49+
50+
return cache[pos][currentMax][remainingCost] = total;
51+
}
52+
53+
let result = 0;
54+
for (let value = 1; value <= maxValue; value++) {
55+
result = (result + computeArrays(1, value, searchCost - 1)) % MOD;
56+
}
57+
58+
return result;
59+
}

0 commit comments

Comments
 (0)