Skip to content

feat: add solutions to lcci problem: No.08.01 #1619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2023
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
363 changes: 306 additions & 57 deletions lcci/08.01.Three Steps Problem/README.md

Large diffs are not rendered by default.

327 changes: 271 additions & 56 deletions lcci/08.01.Three Steps Problem/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,223 @@
```python
class Solution:
def waysToStep(self, n: int) -> int:
if n < 3:
return n
a, b, c = 1, 2, 4
for _ in range(4, n + 1):
a, b, c = b, c, (a + b + c) % 1000000007
return c
mod = 10**9 + 7
for _ in range(n - 1):
a, b, c = b, c, (a + b + c) % mod
return a
```

```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
```

### **Java**

```java
class Solution {
public int waysToStep(int n) {
if (n < 3) {
return n;
final int mod = (int) 1e9 + 7;
int a = 1, b = 2, c = 4;
for (int i = 1; i < n; ++i) {
int t = a;
a = b;
b = c;
c = (((a + b) % mod) + t) % mod;
}
return a;
}
}
```

```java
class Solution {
private final int mod = (int) 1e9 + 7;

public int waysToStep(int n) {
if (n < 4) {
return (int) Math.pow(2, n - 1);
}
long[][] a = {{1, 1, 0}, {1, 0, 1}, {1, 0, 0}};
long[][] res = pow(a, n - 4);
long ans = 0;
for (long x : res[0]) {
ans = (ans + x) % mod;
}
return (int) ans;
}

private long[][] mul(long[][] a, long[][] b) {
int m = a.length, n = b[0].length;
long[][] c = new long[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < b.length; ++k) {
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
}
}
}
return c;
}

private long[][] pow(long[][] a, int n) {
long[][] res = {{4, 2, 1}};
while (n > 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
int waysToStep(int n) {
const int mod = 1e9 + 7;
int a = 1, b = 2, c = 4;
for (int i = 4; i <= n; ++i) {
for (int i = 1; i < n; ++i) {
int t = a;
a = b;
b = c;
c = ((a + b) % 1000000007 + t) % 1000000007;
c = (((a + b) % mod) + t) % mod;
}
return a;
}
};
```

```cpp
class Solution {
public:
int waysToStep(int n) {
if (n < 4) {
return pow(2, n - 1);
}
vector<vector<ll>> a = {{1, 1, 0}, {1, 0, 1}, {1, 0, 0}};
vector<vector<ll>> res = qpow(a, n - 4);
ll ans = 0;
for (ll x : res[0]) {
ans = (ans + x) % mod;
}
return ans;
}

private:
using ll = long long;
const int mod = 1e9 + 7;
vector<vector<ll>> mul(vector<vector<ll>>& a, vector<vector<ll>>& b) {
int m = a.size(), n = b[0].size();
vector<vector<ll>> c(m, vector<ll>(n));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < b.size(); ++k) {
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
}
}
}
return c;
}

vector<vector<ll>> qpow(vector<vector<ll>>& a, int n) {
vector<vector<ll>> res = {{4, 2, 1}};
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
};
```

### **Go**

```go
func waysToStep(n int) int {
const mod int = 1e9 + 7
a, b, c := 1, 2, 4
for i := 1; i < n; i++ {
a, b, c = b, c, (a+b+c)%mod
}
return a
}
```

```go
const mod = 1e9 + 7

func waysToStep(n int) (ans int) {
if n < 4 {
return int(math.Pow(2, float64(n-1)))
}
a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}}
res := pow(a, n-4)
for _, x := range res[0] {
ans = (ans + x) % mod
}
return
}

func mul(a, b [][]int) [][]int {
m, n := len(a), len(b[0])
c := make([][]int, m)
for i := range c {
c[i] = make([]int, n)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < len(b); k++ {
c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod
}
}
}
return c
}

func pow(a [][]int, n int) [][]int {
res := [][]int{{4, 2, 1}}
for n > 0 {
if n&1 == 1 {
res = mul(res, a)
}
a = mul(a, a)
n >>= 1
}
return res
}
```

Expand All @@ -75,74 +267,97 @@ class Solution {
* @return {number}
*/
var waysToStep = function (n) {
if (n < 3) return n;
let a = 1,
b = 2,
c = 4;
for (let i = 3; i < n; i++) {
[a, b, c] = [b, c, (a + b + c) % 1000000007];
let [a, b, c] = [1, 2, 4];
const mod = 1e9 + 7;
for (let i = 1; i < n; ++i) {
[a, b, c] = [b, c, (a + b + c) % mod];
}
return c;
return a;
};
```

### **C**
```js
/**
* @param {number} n
* @return {number}
*/

```c
int waysToStep(int n) {
if (n < 3) {
return n;
const mod = 1e9 + 7;

var waysToStep = function (n) {
if (n < 4) {
return Math.pow(2, n - 1);
}
int a = 1, b = 2, c = 4, i = 4;
while (i++ <= n) {
int t = ((a + b) % 1000000007 + c) % 1000000007;
a = b;
b = c;
c = t;
const a = [
[1, 1, 0],
[1, 0, 1],
[1, 0, 0],
];
let ans = 0;
const res = pow(a, n - 4);
for (const x of res[0]) {
ans = (ans + x) % mod;
}
return ans;
};

function mul(a, b) {
const [m, n] = [a.length, b[0].length];
const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < b.length; ++k) {
c[i][j] =
(c[i][j] + Number((BigInt(a[i][k]) * BigInt(b[k][j])) % BigInt(mod))) % mod;
}
}
}
return c;
}

function pow(a, n) {
let res = [[4, 2, 1]];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
```

### **C++**
### **C**

```cpp
class Solution {
public:
int waysToStep(int n) {
if (n < 3) {
return n;
}
int a = 1, b = 2, c = 4, i = 4;
while (i++ <= n) {
int t = ((a + b) % 1000000007 + c) % 1000000007;
a = b;
b = c;
c = t;
}
return c;
```c
int waysToStep(int n) {
const int mod = 1e9 + 7;
int a = 1, b = 2, c = 4;
for (int i = 1; i < n; ++i) {
int t = a;
a = b;
b = c;
c = (((a + b) % mod) + t) % mod;
}
};
return a;
}
```

### **Rust**

```rust
impl Solution {
pub fn ways_to_step(n: i32) -> i32 {
let mut dp = [1, 2, 4];
let n = n as usize;
if n <= 3 {
return dp[n - 1];
}
for _ in 3..n {
dp = [
dp[1],
dp[2],
(((dp[0] + dp[1]) % 1000000007) + dp[2]) % 1000000007,
];
let (mut a, mut b, mut c) = (1, 2, 4);
let m = 1000000007;
for _ in 1..n {
let t = a;
a = b;
b = c;
c = ((a + b) % m + t) % m;
}
dp[2]
a
}
}
```
Expand Down
Loading