Skip to content

Commit 7ae4aa0

Browse files
committed
Sync LeetCode submission Runtime - 19 ms (96.83%), Memory - 25.1 MB (46.03%)
1 parent d26f8ac commit 7ae4aa0

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<p>You are given two integers <code>n</code> and <code>maxValue</code>, which are used to describe an <strong>ideal</strong> array.</p>
2+
3+
<p>A <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is considered <strong>ideal</strong> if the following conditions hold:</p>
4+
5+
<ul>
6+
<li>Every <code>arr[i]</code> is a value from <code>1</code> to <code>maxValue</code>, for <code>0 &lt;= i &lt; n</code>.</li>
7+
<li>Every <code>arr[i]</code> is divisible by <code>arr[i - 1]</code>, for <code>0 &lt; i &lt; n</code>.</li>
8+
</ul>
9+
10+
<p>Return <em>the number of <strong>distinct</strong> ideal arrays of length </em><code>n</code>. Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> n = 2, maxValue = 5
17+
<strong>Output:</strong> 10
18+
<strong>Explanation:</strong> The following are the possible ideal arrays:
19+
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
20+
- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
21+
- Arrays starting with the value 3 (1 array): [3,3]
22+
- Arrays starting with the value 4 (1 array): [4,4]
23+
- Arrays starting with the value 5 (1 array): [5,5]
24+
There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> n = 5, maxValue = 3
31+
<strong>Output:</strong> 11
32+
<strong>Explanation:</strong> The following are the possible ideal arrays:
33+
- Arrays starting with the value 1 (9 arrays):
34+
- With no other distinct values (1 array): [1,1,1,1,1]
35+
- With 2<sup>nd</sup> distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
36+
- With 2<sup>nd</sup> distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
37+
- Arrays starting with the value 2 (1 array): [2,2,2,2,2]
38+
- Arrays starting with the value 3 (1 array): [3,3,3,3,3]
39+
There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li>
47+
<li><code>1 &lt;= maxValue &lt;= 10<sup>4</sup></code></li>
48+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
MOD = 10**9 + 7
2+
MAX_N = 10**4 + 10
3+
MAX_P = 15
4+
sieve = [0] * MAX_N
5+
6+
for i in range(2, MAX_N):
7+
if sieve[i] == 0:
8+
for j in range(i, MAX_N, i):
9+
sieve[j] = i
10+
11+
ps = [[] for _ in range(MAX_N)]
12+
13+
for i in range(2, MAX_N):
14+
x = i
15+
while x > 1:
16+
p = sieve[x]
17+
cnt = 0
18+
while x % p == 0:
19+
x //= p
20+
cnt += 1
21+
ps[i].append(cnt)
22+
23+
c = [[0] * (MAX_P + 1) for _ in range(MAX_N + MAX_P)]
24+
25+
c[0][0] = 1
26+
for i in range(1, MAX_N + MAX_P):
27+
c[i][0] = 1
28+
for j in range(1, min(i, MAX_P) + 1):
29+
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD
30+
31+
32+
class Solution:
33+
def idealArrays(self, n: int, maxValue: int) -> int:
34+
ans = 0
35+
for x in range(1, maxValue + 1):
36+
mul = 1
37+
for p in ps[x]:
38+
mul = mul * c[n + p - 1][p] % MOD
39+
ans = (ans + mul) % MOD
40+
return ans

0 commit comments

Comments
 (0)