Skip to content

Commit 0ac6df2

Browse files
authored
feat: add solutions to lcci problem: No.08.01 (doocs#1619)
No.08.01.Three Steps Problem
1 parent a12283c commit 0ac6df2

File tree

9 files changed

+791
-186
lines changed

9 files changed

+791
-186
lines changed

lcci/08.01.Three Steps Problem/README.md

Lines changed: 306 additions & 57 deletions
Large diffs are not rendered by default.

lcci/08.01.Three Steps Problem/README_EN.md

Lines changed: 271 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,223 @@
3939
```python
4040
class Solution:
4141
def waysToStep(self, n: int) -> int:
42-
if n < 3:
43-
return n
4442
a, b, c = 1, 2, 4
45-
for _ in range(4, n + 1):
46-
a, b, c = b, c, (a + b + c) % 1000000007
47-
return c
43+
mod = 10**9 + 7
44+
for _ in range(n - 1):
45+
a, b, c = b, c, (a + b + c) % mod
46+
return a
47+
```
48+
49+
```python
50+
class Solution:
51+
def waysToStep(self, n: int) -> int:
52+
mod = 10**9 + 7
53+
54+
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
55+
m, n = len(a), len(b[0])
56+
c = [[0] * n for _ in range(m)]
57+
for i in range(m):
58+
for j in range(n):
59+
for k in range(len(a[0])):
60+
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
61+
return c
62+
63+
def pow(a: List[List[int]], n: int) -> List[List[int]]:
64+
res = [[4, 2, 1]]
65+
while n:
66+
if n & 1:
67+
res = mul(res, a)
68+
n >>= 1
69+
a = mul(a, a)
70+
return res
71+
72+
if n < 4:
73+
return 2 ** (n - 1)
74+
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
75+
return sum(pow(a, n - 4)[0]) % mod
4876
```
4977

5078
### **Java**
5179

5280
```java
5381
class Solution {
5482
public int waysToStep(int n) {
55-
if (n < 3) {
56-
return n;
83+
final int mod = (int) 1e9 + 7;
84+
int a = 1, b = 2, c = 4;
85+
for (int i = 1; i < n; ++i) {
86+
int t = a;
87+
a = b;
88+
b = c;
89+
c = (((a + b) % mod) + t) % mod;
90+
}
91+
return a;
92+
}
93+
}
94+
```
95+
96+
```java
97+
class Solution {
98+
private final int mod = (int) 1e9 + 7;
99+
100+
public int waysToStep(int n) {
101+
if (n < 4) {
102+
return (int) Math.pow(2, n - 1);
103+
}
104+
long[][] a = {{1, 1, 0}, {1, 0, 1}, {1, 0, 0}};
105+
long[][] res = pow(a, n - 4);
106+
long ans = 0;
107+
for (long x : res[0]) {
108+
ans = (ans + x) % mod;
109+
}
110+
return (int) ans;
111+
}
112+
113+
private long[][] mul(long[][] a, long[][] b) {
114+
int m = a.length, n = b[0].length;
115+
long[][] c = new long[m][n];
116+
for (int i = 0; i < m; ++i) {
117+
for (int j = 0; j < n; ++j) {
118+
for (int k = 0; k < b.length; ++k) {
119+
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
120+
}
121+
}
122+
}
123+
return c;
124+
}
125+
126+
private long[][] pow(long[][] a, int n) {
127+
long[][] res = {{4, 2, 1}};
128+
while (n > 0) {
129+
if ((n & 1) == 1) {
130+
res = mul(res, a);
131+
}
132+
a = mul(a, a);
133+
n >>= 1;
57134
}
135+
return res;
136+
}
137+
}
138+
```
139+
140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int waysToStep(int n) {
146+
const int mod = 1e9 + 7;
58147
int a = 1, b = 2, c = 4;
59-
for (int i = 4; i <= n; ++i) {
148+
for (int i = 1; i < n; ++i) {
60149
int t = a;
61150
a = b;
62151
b = c;
63-
c = ((a + b) % 1000000007 + t) % 1000000007;
152+
c = (((a + b) % mod) + t) % mod;
153+
}
154+
return a;
155+
}
156+
};
157+
```
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
int waysToStep(int n) {
163+
if (n < 4) {
164+
return pow(2, n - 1);
165+
}
166+
vector<vector<ll>> a = {{1, 1, 0}, {1, 0, 1}, {1, 0, 0}};
167+
vector<vector<ll>> res = qpow(a, n - 4);
168+
ll ans = 0;
169+
for (ll x : res[0]) {
170+
ans = (ans + x) % mod;
171+
}
172+
return ans;
173+
}
174+
175+
private:
176+
using ll = long long;
177+
const int mod = 1e9 + 7;
178+
vector<vector<ll>> mul(vector<vector<ll>>& a, vector<vector<ll>>& b) {
179+
int m = a.size(), n = b[0].size();
180+
vector<vector<ll>> c(m, vector<ll>(n));
181+
for (int i = 0; i < m; ++i) {
182+
for (int j = 0; j < n; ++j) {
183+
for (int k = 0; k < b.size(); ++k) {
184+
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
185+
}
186+
}
64187
}
65188
return c;
66189
}
190+
191+
vector<vector<ll>> qpow(vector<vector<ll>>& a, int n) {
192+
vector<vector<ll>> res = {{4, 2, 1}};
193+
while (n) {
194+
if (n & 1) {
195+
res = mul(res, a);
196+
}
197+
a = mul(a, a);
198+
n >>= 1;
199+
}
200+
return res;
201+
}
202+
};
203+
```
204+
205+
### **Go**
206+
207+
```go
208+
func waysToStep(n int) int {
209+
const mod int = 1e9 + 7
210+
a, b, c := 1, 2, 4
211+
for i := 1; i < n; i++ {
212+
a, b, c = b, c, (a+b+c)%mod
213+
}
214+
return a
215+
}
216+
```
217+
218+
```go
219+
const mod = 1e9 + 7
220+
221+
func waysToStep(n int) (ans int) {
222+
if n < 4 {
223+
return int(math.Pow(2, float64(n-1)))
224+
}
225+
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
226+
res := pow(a, n-4)
227+
for _, x := range res[0] {
228+
ans = (ans + x) % mod
229+
}
230+
return
231+
}
232+
233+
func mul(a, b [][]int) [][]int {
234+
m, n := len(a), len(b[0])
235+
c := make([][]int, m)
236+
for i := range c {
237+
c[i] = make([]int, n)
238+
}
239+
for i := 0; i < m; i++ {
240+
for j := 0; j < n; j++ {
241+
for k := 0; k < len(b); k++ {
242+
c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod
243+
}
244+
}
245+
}
246+
return c
247+
}
248+
249+
func pow(a [][]int, n int) [][]int {
250+
res := [][]int{{4, 2, 1}}
251+
for n > 0 {
252+
if n&1 == 1 {
253+
res = mul(res, a)
254+
}
255+
a = mul(a, a)
256+
n >>= 1
257+
}
258+
return res
67259
}
68260
```
69261

@@ -75,74 +267,97 @@ class Solution {
75267
* @return {number}
76268
*/
77269
var waysToStep = function (n) {
78-
if (n < 3) return n;
79-
let a = 1,
80-
b = 2,
81-
c = 4;
82-
for (let i = 3; i < n; i++) {
83-
[a, b, c] = [b, c, (a + b + c) % 1000000007];
270+
let [a, b, c] = [1, 2, 4];
271+
const mod = 1e9 + 7;
272+
for (let i = 1; i < n; ++i) {
273+
[a, b, c] = [b, c, (a + b + c) % mod];
84274
}
85-
return c;
275+
return a;
86276
};
87277
```
88278

89-
### **C**
279+
```js
280+
/**
281+
* @param {number} n
282+
* @return {number}
283+
*/
90284

91-
```c
92-
int waysToStep(int n) {
93-
if (n < 3) {
94-
return n;
285+
const mod = 1e9 + 7;
286+
287+
var waysToStep = function (n) {
288+
if (n < 4) {
289+
return Math.pow(2, n - 1);
95290
}
96-
int a = 1, b = 2, c = 4, i = 4;
97-
while (i++ <= n) {
98-
int t = ((a + b) % 1000000007 + c) % 1000000007;
99-
a = b;
100-
b = c;
101-
c = t;
291+
const a = [
292+
[1, 1, 0],
293+
[1, 0, 1],
294+
[1, 0, 0],
295+
];
296+
let ans = 0;
297+
const res = pow(a, n - 4);
298+
for (const x of res[0]) {
299+
ans = (ans + x) % mod;
300+
}
301+
return ans;
302+
};
303+
304+
function mul(a, b) {
305+
const [m, n] = [a.length, b[0].length];
306+
const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
307+
for (let i = 0; i < m; ++i) {
308+
for (let j = 0; j < n; ++j) {
309+
for (let k = 0; k < b.length; ++k) {
310+
c[i][j] =
311+
(c[i][j] + Number((BigInt(a[i][k]) * BigInt(b[k][j])) % BigInt(mod))) % mod;
312+
}
313+
}
102314
}
103315
return c;
104316
}
317+
318+
function pow(a, n) {
319+
let res = [[4, 2, 1]];
320+
while (n) {
321+
if (n & 1) {
322+
res = mul(res, a);
323+
}
324+
a = mul(a, a);
325+
n >>= 1;
326+
}
327+
return res;
328+
}
105329
```
106330

107-
### **C++**
331+
### **C**
108332

109-
```cpp
110-
class Solution {
111-
public:
112-
int waysToStep(int n) {
113-
if (n < 3) {
114-
return n;
115-
}
116-
int a = 1, b = 2, c = 4, i = 4;
117-
while (i++ <= n) {
118-
int t = ((a + b) % 1000000007 + c) % 1000000007;
119-
a = b;
120-
b = c;
121-
c = t;
122-
}
123-
return c;
333+
```c
334+
int waysToStep(int n) {
335+
const int mod = 1e9 + 7;
336+
int a = 1, b = 2, c = 4;
337+
for (int i = 1; i < n; ++i) {
338+
int t = a;
339+
a = b;
340+
b = c;
341+
c = (((a + b) % mod) + t) % mod;
124342
}
125-
};
343+
return a;
344+
}
126345
```
127346
128347
### **Rust**
129348
130349
```rust
131350
impl Solution {
132351
pub fn ways_to_step(n: i32) -> i32 {
133-
let mut dp = [1, 2, 4];
134-
let n = n as usize;
135-
if n <= 3 {
136-
return dp[n - 1];
137-
}
138-
for _ in 3..n {
139-
dp = [
140-
dp[1],
141-
dp[2],
142-
(((dp[0] + dp[1]) % 1000000007) + dp[2]) % 1000000007,
143-
];
352+
let (mut a, mut b, mut c) = (1, 2, 4);
353+
let m = 1000000007;
354+
for _ in 1..n {
355+
let t = a;
356+
a = b;
357+
b = c;
358+
c = ((a + b) % m + t) % m;
144359
}
145-
dp[2]
360+
a
146361
}
147362
}
148363
```

0 commit comments

Comments
 (0)