Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1011501

Browse files
feat: add solutions to lc/lcci problems (doocs#1620)
1 parent 0ac6df2 commit 1011501

File tree

11 files changed

+220
-74
lines changed

11 files changed

+220
-74
lines changed

‎lcci/08.01.Three Steps Problem/README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ class Solution:
113113
return sum(pow(a, n - 4)[0]) % mod
114114
```
115115

116+
```python
117+
import numpy as np
118+
119+
120+
class Solution:
121+
def waysToStep(self, n: int) -> int:
122+
if n < 4:
123+
return 2 ** (n - 1)
124+
mod = 10**9 + 7
125+
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
126+
res = np.mat([(4, 2, 1)], np.dtype("O"))
127+
n -= 4
128+
while n:
129+
if n & 1:
130+
res = res * factor % mod
131+
factor = factor * factor % mod
132+
n >>= 1
133+
return res.sum() % mod
134+
```
135+
116136
### **Java**
117137

118138
<!-- 这里可写当前语言的特殊实现逻辑 -->

‎lcci/08.01.Three Steps Problem/README_EN.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ class Solution:
7575
return sum(pow(a, n - 4)[0]) % mod
7676
```
7777

78+
```python
79+
import numpy as np
80+
81+
82+
class Solution:
83+
def waysToStep(self, n: int) -> int:
84+
if n < 4:
85+
return 2 ** (n - 1)
86+
mod = 10**9 + 7
87+
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
88+
res = np.mat([(4, 2, 1)], np.dtype("O"))
89+
n -= 4
90+
while n:
91+
if n & 1:
92+
res = res * factor % mod
93+
factor = factor * factor % mod
94+
n >>= 1
95+
return res.sum() % mod
96+
```
97+
7898
### **Java**
7999

80100
```java
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
class Solution:
2-
def waysToStep(self, n: int) -> int:
3-
mod = 10**9 + 7
4-
5-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
6-
m, n = len(a), len(b[0])
7-
c = [[0] * n for _ in range(m)]
8-
for i in range(m):
9-
for j in range(n):
10-
for k in range(len(a[0])):
11-
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
12-
return c
1+
import numpy as np
132

14-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
15-
res = [[4, 2, 1]]
16-
while n:
17-
if n & 1:
18-
res = mul(res, a)
19-
n >>= 1
20-
a = mul(a, a)
21-
return res
223

4+
class Solution:
5+
def waysToStep(self, n: int) -> int:
236
if n < 4:
247
return 2 ** (n - 1)
25-
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
26-
return sum(pow(a, n - 4)[0]) % mod
8+
mod = 10**9 + 7
9+
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
10+
res = np.mat([(4, 2, 1)], np.dtype("O"))
11+
n -= 4
12+
while n:
13+
if n & 1:
14+
res = res * factor % mod
15+
factor = factor * factor % mod
16+
n >>= 1
17+
return res.sum() % mod

‎solution/0000-0099/0070.Climbing Stairs/README.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $$
6060

6161
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。
6262

63-
**方法二:矩阵快速幂**
63+
**方法二:矩阵快速幂加速递推**
6464

6565
我们设 $Fib(n)$ 表示一个 1ドル \times 2$ 的矩阵 $\begin{bmatrix} F_n & F_{n - 1} \end{bmatrix},ドル其中 $F_n$ 和 $F_{n - 1}$ 分别是第 $n$ 个和第 $n - 1$ 个斐波那契数。
6666

@@ -128,7 +128,7 @@ class Solution:
128128
return c
129129

130130
def pow(a: List[List[int]], n: int) -> List[List[int]]:
131-
res = [[1, 1], [0, 0]]
131+
res = [[1, 1]]
132132
while n:
133133
if n & 1:
134134
res = mul(res, a)
@@ -140,6 +140,23 @@ class Solution:
140140
return pow(a, n - 1)[0][0]
141141
```
142142

143+
```python
144+
import numpy as np
145+
146+
147+
class Solution:
148+
def climbStairs(self, n: int) -> int:
149+
res = np.mat([(1, 1)], np.dtype("O"))
150+
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
151+
n -= 1
152+
while n:
153+
if n & 1:
154+
res *= factor
155+
factor *= factor
156+
n >>= 1
157+
return res[0, 0]
158+
```
159+
143160
### **Java**
144161

145162
<!-- 这里可写当前语言的特殊实现逻辑 -->

‎solution/0000-0099/0070.Climbing Stairs/README_EN.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Solution:
6565
return c
6666

6767
def pow(a: List[List[int]], n: int) -> List[List[int]]:
68-
res = [[1, 1], [0, 0]]
68+
res = [[1, 1]]
6969
while n:
7070
if n & 1:
7171
res = mul(res, a)
@@ -77,6 +77,23 @@ class Solution:
7777
return pow(a, n - 1)[0][0]
7878
```
7979

80+
```python
81+
import numpy as np
82+
83+
84+
class Solution:
85+
def climbStairs(self, n: int) -> int:
86+
res = np.mat([(1, 1)], np.dtype("O"))
87+
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
88+
n -= 1
89+
while n:
90+
if n & 1:
91+
res *= factor
92+
factor *= factor
93+
n >>= 1
94+
return res[0, 0]
95+
```
96+
8097
### **Java**
8198

8299
```java

‎solution/1100-1199/1137.N-th Tribonacci Number/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ class Solution:
122122
return sum(pow(a, n - 3)[0])
123123
```
124124

125+
```python
126+
import numpy as np
127+
128+
129+
class Solution:
130+
def tribonacci(self, n: int) -> int:
131+
if n == 0:
132+
return 0
133+
if n < 3:
134+
return 1
135+
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
136+
res = np.mat([(1, 1, 0)], np.dtype("O"))
137+
n -= 3
138+
while n:
139+
if n & 1:
140+
res *= factor
141+
factor *= factor
142+
n >>= 1
143+
return res.sum()
144+
```
145+
125146
### **Java**
126147

127148
<!-- 这里可写当前语言的特殊实现逻辑 -->

‎solution/1100-1199/1137.N-th Tribonacci Number/README_EN.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ class Solution:
8080
return sum(pow(a, n - 3)[0])
8181
```
8282

83+
```python
84+
import numpy as np
85+
86+
87+
class Solution:
88+
def tribonacci(self, n: int) -> int:
89+
if n == 0:
90+
return 0
91+
if n < 3:
92+
return 1
93+
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
94+
res = np.mat([(1, 1, 0)], np.dtype("O"))
95+
n -= 3
96+
while n:
97+
if n & 1:
98+
res *= factor
99+
factor *= factor
100+
n >>= 1
101+
return res.sum()
102+
```
103+
83104
### **Java**
84105

85106
```java
Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
class Solution:
2-
def tribonacci(self, n: int) -> int:
3-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
4-
m, n = len(a), len(b[0])
5-
c = [[0] * n for _ in range(m)]
6-
for i in range(m):
7-
for j in range(n):
8-
for k in range(len(a[0])):
9-
c[i][j] = c[i][j] + a[i][k] * b[k][j]
10-
return c
1+
import numpy as np
112

12-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
13-
res = [[1, 1, 0]]
14-
while n:
15-
if n & 1:
16-
res = mul(res, a)
17-
n >>= 1
18-
a = mul(a, a)
19-
return res
203

4+
class Solution:
5+
def tribonacci(self, n: int) -> int:
216
if n == 0:
227
return 0
238
if n < 3:
249
return 1
25-
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
26-
return sum(pow(a, n - 3)[0])
10+
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
11+
res = np.mat([(1, 1, 0)], np.dtype("O"))
12+
n -= 3
13+
while n:
14+
if n & 1:
15+
res *= factor
16+
factor *= factor
17+
n >>= 1
18+
return res.sum()

‎solution/1200-1299/1220.Count Vowels Permutation/README.md‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ $$
9191

9292
时间复杂度 $O(n),ドル空间复杂度 $O(C)$。其中 $n$ 是字符串的长度,而 $C$ 是元音字母的个数。本题中 $C=5$。
9393

94-
**方法二:矩阵快速幂**
94+
**方法二:矩阵快速幂加速递推**
9595

9696
时间复杂度 $O(C^3 \times \log n),ドル空间复杂度 $O(C^2),ドル其中 $C$ 是元音字母的个数,本题中 $C=5$。
9797

@@ -151,6 +151,33 @@ class Solution:
151151
return sum(map(sum, res)) % mod
152152
```
153153

154+
```python
155+
import numpy as np
156+
157+
158+
class Solution:
159+
def countVowelPermutation(self, n: int) -> int:
160+
mod = 10**9 + 7
161+
factor = np.mat(
162+
[
163+
(0, 1, 0, 0, 0),
164+
(1, 0, 1, 0, 0),
165+
(1, 1, 0, 1, 1),
166+
(0, 0, 1, 0, 1),
167+
(1, 0, 0, 0, 0),
168+
],
169+
np.dtype("O"),
170+
)
171+
res = np.mat([(1, 1, 1, 1, 1)], np.dtype("O"))
172+
n -= 1
173+
while n:
174+
if n & 1:
175+
res = res * factor % mod
176+
factor = factor * factor % mod
177+
n >>= 1
178+
return res.sum() % mod
179+
```
180+
154181
### **Java**
155182

156183
<!-- 这里可写当前语言的特殊实现逻辑 -->

‎solution/1200-1299/1220.Count Vowels Permutation/README_EN.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,33 @@ class Solution:
119119
return sum(map(sum, res)) % mod
120120
```
121121

122+
```python
123+
import numpy as np
124+
125+
126+
class Solution:
127+
def countVowelPermutation(self, n: int) -> int:
128+
mod = 10**9 + 7
129+
factor = np.mat(
130+
[
131+
(0, 1, 0, 0, 0),
132+
(1, 0, 1, 0, 0),
133+
(1, 1, 0, 1, 1),
134+
(0, 0, 1, 0, 1),
135+
(1, 0, 0, 0, 0),
136+
],
137+
np.dtype("O"),
138+
)
139+
res = np.mat([(1, 1, 1, 1, 1)], np.dtype("O"))
140+
n -= 1
141+
while n:
142+
if n & 1:
143+
res = res * factor % mod
144+
factor = factor * factor % mod
145+
n >>= 1
146+
return res.sum() % mod
147+
```
148+
122149
### **Java**
123150

124151
```java

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /