Skip to content

Commit 6cc653d

Browse files
committedApr 22, 2025
Add solution #2338
1 parent 344ae7d commit 6cc653d

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-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,415 LeetCode solutions in JavaScript
1+
# 1,416 LeetCode solutions in JavaScript
22

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

@@ -1311,6 +1311,7 @@
13111311
2270|[Number of Ways to Split Array](./solutions/2270-number-of-ways-to-split-array.js)|Medium|
13121312
2300|[Successful Pairs of Spells and Potions](./solutions/2300-successful-pairs-of-spells-and-potions.js)|Medium|
13131313
2336|[Smallest Number in Infinite Set](./solutions/2336-smallest-number-in-infinite-set.js)|Medium|
1314+
2338|[Count the Number of Ideal Arrays](./solutions/2338-count-the-number-of-ideal-arrays.js)|Hard|
13141315
2342|[Max Sum of a Pair With Equal Sum of Digits](./solutions/2342-max-sum-of-a-pair-with-equal-sum-of-digits.js)|Medium|
13151316
2349|[Design a Number Container System](./solutions/2349-design-a-number-container-system.js)|Medium|
13161317
2352|[Equal Row and Column Pairs](./solutions/2352-equal-row-and-column-pairs.js)|Medium|
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* 2338. Count the Number of Ideal Arrays
3+
* https://leetcode.com/problems/count-the-number-of-ideal-arrays/
4+
* Difficulty: Hard
5+
*
6+
* You are given two integers n and maxValue, which are used to describe an ideal array.
7+
*
8+
* A 0-indexed integer array arr of length n is considered ideal if the following conditions hold:
9+
* - Every arr[i] is a value from 1 to maxValue, for 0 <= i < n.
10+
* - Every arr[i] is divisible by arr[i - 1], for 0 < i < n.
11+
*
12+
* Return the number of distinct ideal arrays of length n. Since the answer may be very large,
13+
* return it modulo 109 + 7.
14+
*/
15+
16+
/**
17+
* @param {number} n
18+
* @param {number} maxValue
19+
* @return {number}
20+
*/
21+
var idealArrays = function(n, maxValue) {
22+
const MOD = 1e9 + 7;
23+
const MAX_N = 10010;
24+
const MAX_P = 15;
25+
26+
const c = Array.from({ length: MAX_N + MAX_P }, () =>
27+
new Array(MAX_P + 1).fill(0)
28+
);
29+
const sieve = new Array(MAX_N).fill(0);
30+
const ps = Array.from({ length: MAX_N }, () => []);
31+
32+
for (let i = 2; i < MAX_N; i++) {
33+
if (sieve[i] === 0) {
34+
for (let j = i; j < MAX_N; j += i) {
35+
if (sieve[j] === 0) {
36+
sieve[j] = i;
37+
}
38+
}
39+
}
40+
}
41+
42+
for (let i = 2; i < MAX_N; i++) {
43+
let x = i;
44+
while (x > 1) {
45+
const p = sieve[x];
46+
let cnt = 0;
47+
while (x % p === 0) {
48+
x = Math.floor(x / p);
49+
cnt++;
50+
}
51+
ps[i].push(cnt);
52+
}
53+
}
54+
55+
c[0][0] = 1;
56+
for (let i = 1; i < MAX_N + MAX_P; i++) {
57+
c[i][0] = 1;
58+
for (let j = 1; j <= Math.min(i, MAX_P); j++) {
59+
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;
60+
}
61+
}
62+
63+
let ans = 0n;
64+
for (let x = 1; x <= maxValue; x++) {
65+
let mul = 1n;
66+
for (const p of ps[x]) {
67+
mul = (mul * BigInt(c[n + p - 1][p])) % BigInt(MOD);
68+
}
69+
ans = (ans + mul) % BigInt(MOD);
70+
}
71+
72+
return Number(ans);
73+
};

0 commit comments

Comments
 (0)
Please sign in to comment.