Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 3 additions & 34 deletions lcci/08.01.Three Steps Problem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.01.Three%20Steps%2
<p> <strong>示例1:</strong></p>

<pre>
<strong> 输入</strong>:n = 3
<strong> 输入</strong>:n = 3
<strong> 输出</strong>:4
<strong> 说明</strong>: 有四种走法
</pre>
Expand Down Expand Up @@ -226,37 +226,6 @@ $$

#### Python3

```python
class Solution:
def waysToStep(self, n: int) -> int:
mod = 10**9 + 7

def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
return c

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[4, 2, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

if n < 4:
return 2 ** (n - 1)
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
return sum(pow(a, n - 4)[0]) % mod
```

#### Python3

```python
import numpy as np

Expand All @@ -266,8 +235,8 @@ class Solution:
if n < 4:
return 2 ** (n - 1)
mod = 10**9 + 7
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
res = np.mat([(4, 2, 1)], np.dtype("O"))
factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
n -= 4
while n:
if n & 1:
Expand Down
37 changes: 3 additions & 34 deletions lcci/08.01.Three Steps Problem/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.01.Three%20Steps%2

<pre>

<strong> Input</strong>: n = 3
<strong> Input</strong>: n = 3

<strong> Output</strong>: 4

Expand Down Expand Up @@ -229,37 +229,6 @@ The time complexity is $O(\log n)$, and the space complexity is $O(1)$.

#### Python3

```python
class Solution:
def waysToStep(self, n: int) -> int:
mod = 10**9 + 7

def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
return c

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[4, 2, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

if n < 4:
return 2 ** (n - 1)
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
return sum(pow(a, n - 4)[0]) % mod
```

#### Python3

```python
import numpy as np

Expand All @@ -269,8 +238,8 @@ class Solution:
if n < 4:
return 2 ** (n - 1)
mod = 10**9 + 7
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
res = np.mat([(4, 2, 1)], np.dtype("O"))
factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
n -= 4
while n:
if n & 1:
Expand Down
35 changes: 13 additions & 22 deletions lcci/08.01.Three Steps Problem/Solution2.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
class Solution:
def waysToStep(self, n: int) -> int:
mod = 10**9 + 7

def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
return c
import numpy as np

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[4, 2, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

class Solution:
def waysToStep(self, n: int) -> int:
if n < 4:
return 2 ** (n - 1)
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
return sum(pow(a, n - 4)[0]) % mod
mod = 10**9 + 7
factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
n -= 4
while n:
if n & 1:
res = res * factor % mod
factor = factor * factor % mod
n >>= 1
return res.sum() % mod
17 changes: 0 additions & 17 deletions lcci/08.01.Three Steps Problem/Solution3.py

This file was deleted.

61 changes: 12 additions & 49 deletions solution/0000-0099/0070.Climbing Stairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,28 +252,20 @@ $$
#### Python3

```python
import numpy as np


class Solution:
def climbStairs(self, n: int) -> int:
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
return c

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[1, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

a = [[1, 1], [1, 0]]
return pow(a, n - 1)[0][0]
res = np.asmatrix([(1, 1)], np.dtype("O"))
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]
```

#### Java
Expand Down Expand Up @@ -478,33 +470,4 @@ function pow(a, n) {

<!-- solution:end -->

<!-- solution:start -->

### 方法三

<!-- tabs:start -->

#### Python3

```python
import numpy as np


class Solution:
def climbStairs(self, n: int) -> int:
res = np.mat([(1, 1)], np.dtype("O"))
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
61 changes: 12 additions & 49 deletions solution/0000-0099/0070.Climbing Stairs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,20 @@ The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
#### Python3

```python
import numpy as np


class Solution:
def climbStairs(self, n: int) -> int:
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
return c

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[1, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

a = [[1, 1], [1, 0]]
return pow(a, n - 1)[0][0]
res = np.asmatrix([(1, 1)], np.dtype("O"))
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]
```

#### Java
Expand Down Expand Up @@ -477,33 +469,4 @@ function pow(a, n) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 3

<!-- tabs:start -->

#### Python3

```python
import numpy as np


class Solution:
def climbStairs(self, n: int) -> int:
res = np.mat([(1, 1)], np.dtype("O"))
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
32 changes: 12 additions & 20 deletions solution/0000-0099/0070.Climbing Stairs/Solution2.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
class Solution:
def climbStairs(self, n: int) -> int:
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n = len(a), len(b[0])
c = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
for k in range(len(a[0])):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
return c
import numpy as np

def pow(a: List[List[int]], n: int) -> List[List[int]]:
res = [[1, 1]]
while n:
if n & 1:
res = mul(res, a)
n >>= 1
a = mul(a, a)
return res

a = [[1, 1], [1, 0]]
return pow(a, n - 1)[0][0]
class Solution:
def climbStairs(self, n: int) -> int:
res = np.asmatrix([(1, 1)], np.dtype("O"))
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]
14 changes: 0 additions & 14 deletions solution/0000-0099/0070.Climbing Stairs/Solution3.py

This file was deleted.

Loading
Loading