Skip to content

Commit 2c3fe09

Browse files
committed
Sync LeetCode submission Runtime - 16 ms (89.06%), Memory - 25.2 MB (25.00%)
1 parent 90cb4a7 commit 2c3fe09

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<p>You are given three integers <code>n</code>, <code>m</code>, <code>k</code>. A <strong>good array</strong> <code>arr</code> of size <code>n</code> is defined as follows:</p>
2+
3+
<ul>
4+
<li>Each element in <code>arr</code> is in the <strong>inclusive</strong> range <code>[1, m]</code>.</li>
5+
<li><em>Exactly</em> <code>k</code> indices <code>i</code> (where <code>1 &lt;= i &lt; n</code>) satisfy the condition <code>arr[i - 1] == arr[i]</code>.</li>
6+
</ul>
7+
8+
<p>Return the number of <strong>good arrays</strong> that can be formed.</p>
9+
10+
<p>Since the answer may be very large, return it <strong>modulo </strong><code>10<sup>9 </sup>+ 7</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<div class="example-block">
16+
<p><strong>Input:</strong> <span class="example-io">n = 3, m = 2, k = 1</span></p>
17+
18+
<p><strong>Output:</strong> <span class="example-io">4</span></p>
19+
20+
<p><strong>Explanation:</strong></p>
21+
22+
<ul>
23+
<li>There are 4 good arrays. They are <code>[1, 1, 2]</code>, <code>[1, 2, 2]</code>, <code>[2, 1, 1]</code> and <code>[2, 2, 1]</code>.</li>
24+
<li>Hence, the answer is 4.</li>
25+
</ul>
26+
</div>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">n = 4, m = 2, k = 2</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
34+
35+
<p><strong>Explanation:</strong></p>
36+
37+
<ul>
38+
<li>The good arrays are <code>[1, 1, 1, 2]</code>, <code>[1, 1, 2, 2]</code>, <code>[1, 2, 2, 2]</code>, <code>[2, 1, 1, 1]</code>, <code>[2, 2, 1, 1]</code> and <code>[2, 2, 2, 1]</code>.</li>
39+
<li>Hence, the answer is 6.</li>
40+
</ul>
41+
</div>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
45+
<div class="example-block">
46+
<p><strong>Input:</strong> <span class="example-io">n = 5, m = 2, k = 0</span></p>
47+
48+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
49+
50+
<p><strong>Explanation:</strong></p>
51+
52+
<ul>
53+
<li>The good arrays are <code>[1, 2, 1, 2, 1]</code> and <code>[2, 1, 2, 1, 2]</code>. Hence, the answer is 2.</li>
54+
</ul>
55+
</div>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
62+
<li><code>1 &lt;= m &lt;= 10<sup>5</sup></code></li>
63+
<li><code>0 &lt;= k &lt;= n - 1</code></li>
64+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
MOD = 10**9 + 7
2+
MX = 10**5
3+
4+
fact = [0] * MX
5+
inv_fact = [0] * MX
6+
7+
8+
def qpow(x, n):
9+
res = 1
10+
while n:
11+
if n & 1:
12+
res = res * x % MOD
13+
x = x * x % MOD
14+
n >>= 1
15+
return res
16+
17+
18+
def init():
19+
if fact[0] != 0:
20+
return
21+
fact[0] = 1
22+
for i in range(1, MX):
23+
fact[i] = fact[i - 1] * i % MOD
24+
inv_fact[MX - 1] = qpow(fact[MX - 1], MOD - 2)
25+
for i in range(MX - 1, 0, -1):
26+
inv_fact[i - 1] = inv_fact[i] * i % MOD
27+
28+
29+
def comb(n, m):
30+
return fact[n] * inv_fact[m] % MOD * inv_fact[n - m] % MOD
31+
32+
33+
class Solution:
34+
def countGoodArrays(self, n: int, m: int, k: int) -> int:
35+
init()
36+
return comb(n - 1, k) * m % MOD * qpow(m - 1, n - k - 1) % MOD

0 commit comments

Comments
 (0)