Skip to content

[pull] main from doocs:main #123

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 31 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ee7ddb9
feat: add solutions to lc problems: No.3151~3153 (#2838)
yanglbme May 19, 2024
3f6a34f
feat: add solutions to lc problem: No.3154 (#2842)
yanglbme May 19, 2024
51b984f
feat: add cpp solution to lc problem: No.3068 (#2839)
AlleyNawaz May 19, 2024
f55b0d7
feat: add cpp solution to lc problem: No.3068 (#2840)
AlleyNawaz May 19, 2024
ac3102d
feat: add cpp solution to lc problem: No.3068 (#2841)
AlleyNawaz May 19, 2024
818daa9
feat: add solutions to lc problem: No.3155 (#2843)
yanglbme May 19, 2024
06533ad
feat: add solutions to lc problem: No.1317 (#2844)
yanglbme May 19, 2024
41ebef4
refactor: update solution with sorting via object to lc problem: No.2…
rain84 May 20, 2024
aa8ed86
refactor: update solution to lc problem: No.2625 (#2848)
rain84 May 20, 2024
dfb0297
feat: add ts solution to lc problem: No.1542 (#2850)
yanglbme May 20, 2024
5e67f9f
feat: update solutions to lc problem: No.2705 (#2851)
yanglbme May 20, 2024
5301c76
feat: update lc problems (#2852)
yanglbme May 20, 2024
4db3cd4
feat: add swift implementation to lcof problem: No.06 (#2853)
klever34 May 20, 2024
d2ee6aa
feat: add swift implementation to lcof problem: No.07 (#2854)
klever34 May 20, 2024
5cc6315
feat: add swift implementation to lcof problem: No.09 (#2855)
klever34 May 20, 2024
d451882
feat: add swift implementation to lcof problem: No.10.1 (#2856)
klever34 May 20, 2024
98beaf4
feat: add swift implementation to lcof problem: No.10.2 (#2857)
klever34 May 20, 2024
5f6a3af
feat: add swift implementation to lcof problem: No.11 (#2858)
klever34 May 20, 2024
df9bd5d
feat: add sql solution to lc problem: No.3156 (#2859)
yanglbme May 20, 2024
14c8085
feat: add solutions to lcp problem: No.80 (#2860)
yanglbme May 20, 2024
feae0f6
feat: update solutions to lc/lcof problems (#2861)
yanglbme May 21, 2024
27036e8
feat: update solutions to lcof problem: No.15 (#2862)
yanglbme May 21, 2024
6bb333d
--- (#2863)
dependabot[bot] May 21, 2024
a9caf56
feat: add swift implementation to lcof problem: No.12 (#2864)
klever34 May 21, 2024
d449a8a
feat: add swift implementation to lcof problem: No.13 (#2865)
klever34 May 21, 2024
7796a59
feat: add swift implementation to lcof problem: No.14.1 (#2866)
klever34 May 21, 2024
fc09c8c
feat: add swift implementation to lcof problem: No.14.2 (#2867)
klever34 May 21, 2024
d6c9b4f
feat: add swift implementation to lcof problem: No.15 (#2868)
klever34 May 21, 2024
c954041
feat: add swift implementation to lcof problem: No.16 (#2869)
klever34 May 21, 2024
3c5c2d6
feat: add solutions to lc problem: No.2831 (#2870)
yanglbme May 21, 2024
3768553
feat: update solutions to lcof problems: No.26,27,30 (#2871)
yanglbme May 21, 2024
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
Prev Previous commit
Next Next commit
feat: add solutions to lc problem: No.3154 (doocs#2842)
No.3154.Find Number of Ways to Reach the K-th Stair
  • Loading branch information
yanglbme authored May 19, 2024
commit 3f6a34fd7f44b298dfdb4b4f09bd6b856d64748d
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,166 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3154.Fi

<!-- solution:start -->

### 方法一
### 方法一:记忆化搜索

我们设计一个函数 $\text{dfs}(i, j, \text{jump})$,表示当前位于第 $i$ 级台阶,且进行了 $j$ 次操作 $1$ 和 $\text{jump}$ 次操作 $2$,到达第 $k$ 级台阶的方案数。那么答案就是 $\text{dfs}(1, 0, 0)$。

函数 $\text{dfs}(i, j, \text{jump})$ 的计算过程如下:

- 如果 $i > k + 1$,由于无法连续两次向下走,所以无法再到达第 $k$ 级台阶,返回 $0$;
- 如果 $i = k$,表示已经到达第 $k$ 级台阶,答案初始化为 $1$,然后继续计算;
- 如果 $i > 0$ 且 $j = 0$,表示可以向下走,递归计算 $\text{dfs}(i - 1, 1, \text{jump})$;
- 递归计算 $\text{dfs}(i + 2^{\text{jump}}, 0, \text{jump} + 1)$,累加到答案中。

为了避免重复计算,我们使用记忆化搜索,将已经计算过的状态保存起来。

时间复杂度 $(\log ^2 k)$,空间复杂度 $(\log ^2 k)$。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def waysToReachStair(self, k: int) -> int:
@cache
def dfs(i: int, j: int, jump: int) -> int:
if i > k + 1:
return 0
ans = int(i == k)
if i > 0 and j == 0:
ans += dfs(i - 1, 1, jump)
ans += dfs(i + (1 << jump), 0, jump + 1)
return ans

return dfs(1, 0, 0)
```

#### Java

```java

class Solution {
private Map<Long, Integer> f = new HashMap<>();
private int k;

public int waysToReachStair(int k) {
this.k = k;
return dfs(1, 0, 0);
}

private int dfs(int i, int j, int jump) {
if (i > k + 1) {
return 0;
}
long key = ((long) i << 32) | jump << 1 | j;
if (f.containsKey(key)) {
return f.get(key);
}
int ans = i == k ? 1 : 0;
if (i > 0 && j == 0) {
ans += dfs(i - 1, 1, jump);
}
ans += dfs(i + (1 << jump), 0, jump + 1);
f.put(key, ans);
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int waysToReachStair(int k) {
this->k = k;
return dfs(1, 0, 0);
}

private:
unordered_map<long long, int> f;
int k;

int dfs(int i, int j, int jump) {
if (i > k + 1) {
return 0;
}
long long key = ((long long) i << 32) | jump << 1 | j;
if (f.contains(key)) {
return f[key];
}
int ans = i == k ? 1 : 0;
if (i > 0 && j == 0) {
ans += dfs(i - 1, 1, jump);
}
ans += dfs(i + (1 << jump), 0, jump + 1);
f[key] = ans;
return ans;
}
};
```

#### Go

```go
func waysToReachStair(k int) int {
f := map[int]int{}
var dfs func(i, j, jump int) int
dfs = func(i, j, jump int) int {
if i > k+1 {
return 0
}
key := (i << 32) | jump<<1 | j
if v, has := f[key]; has {
return v
}
ans := 0
if i == k {
ans++
}
if i > 0 && j == 0 {
ans += dfs(i-1, 1, jump)
}
ans += dfs(i+(1<<jump), 0, jump+1)
f[key] = ans
return ans
}
return dfs(1, 0, 0)
}
```

#### TypeScript

```ts
function waysToReachStair(k: number): number {
const f: Map<bigint, number> = new Map();

const dfs = (i: number, j: number, jump: number): number => {
if (i > k + 1) {
return 0;
}

const key: bigint = (BigInt(i) << BigInt(32)) | BigInt(jump << 1) | BigInt(j);
if (f.has(key)) {
return f.get(key)!;
}

let ans: number = 0;
if (i === k) {
ans++;
}

if (i > 0 && j === 0) {
ans += dfs(i - 1, 1, jump);
}

ans += dfs(i + (1 << jump), 0, jump + 1);
f.set(key, ans);
return ans;
};

return dfs(1, 0, 0);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,166 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3154.Fi

<!-- solution:start -->

### Solution 1
### Solution 1: Memoization Search

We design a function `dfs(i, j, jump)`, which represents the number of ways to reach the $k$th step when currently at the $i$th step, having performed $j$ operation 1's and `jump` operation 2's. The answer is `dfs(1, 0, 0)`.

The calculation process of the function `dfs(i, j, jump)` is as follows:

- If $i > k + 1$, since we cannot go down twice in a row, we cannot reach the $k$th step again, so return $0$;
- If $i = k$, it means that we have reached the $k$th step. The answer is initialized to $1$, and then continue to calculate;
- If $i > 0$ and $j = 0$, it means that we can go down, recursively calculate `dfs(i - 1, 1, jump)`;
- Recursively calculate `dfs(i + 2^{jump}, 0, jump + 1)`, and add it to the answer.

To avoid repeated calculations, we use memoization search to save the calculated states.

The time complexity is $O(\log^2 k)$, and the space complexity is $O(\log^2 k)$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def waysToReachStair(self, k: int) -> int:
@cache
def dfs(i: int, j: int, jump: int) -> int:
if i > k + 1:
return 0
ans = int(i == k)
if i > 0 and j == 0:
ans += dfs(i - 1, 1, jump)
ans += dfs(i + (1 << jump), 0, jump + 1)
return ans

return dfs(1, 0, 0)
```

#### Java

```java

class Solution {
private Map<Long, Integer> f = new HashMap<>();
private int k;

public int waysToReachStair(int k) {
this.k = k;
return dfs(1, 0, 0);
}

private int dfs(int i, int j, int jump) {
if (i > k + 1) {
return 0;
}
long key = ((long) i << 32) | jump << 1 | j;
if (f.containsKey(key)) {
return f.get(key);
}
int ans = i == k ? 1 : 0;
if (i > 0 && j == 0) {
ans += dfs(i - 1, 1, jump);
}
ans += dfs(i + (1 << jump), 0, jump + 1);
f.put(key, ans);
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int waysToReachStair(int k) {
this->k = k;
return dfs(1, 0, 0);
}

private:
unordered_map<long long, int> f;
int k;

int dfs(int i, int j, int jump) {
if (i > k + 1) {
return 0;
}
long long key = ((long long) i << 32) | jump << 1 | j;
if (f.contains(key)) {
return f[key];
}
int ans = i == k ? 1 : 0;
if (i > 0 && j == 0) {
ans += dfs(i - 1, 1, jump);
}
ans += dfs(i + (1 << jump), 0, jump + 1);
f[key] = ans;
return ans;
}
};
```

#### Go

```go
func waysToReachStair(k int) int {
f := map[int]int{}
var dfs func(i, j, jump int) int
dfs = func(i, j, jump int) int {
if i > k+1 {
return 0
}
key := (i << 32) | jump<<1 | j
if v, has := f[key]; has {
return v
}
ans := 0
if i == k {
ans++
}
if i > 0 && j == 0 {
ans += dfs(i-1, 1, jump)
}
ans += dfs(i+(1<<jump), 0, jump+1)
f[key] = ans
return ans
}
return dfs(1, 0, 0)
}
```

#### TypeScript

```ts
function waysToReachStair(k: number): number {
const f: Map<bigint, number> = new Map();

const dfs = (i: number, j: number, jump: number): number => {
if (i > k + 1) {
return 0;
}

const key: bigint = (BigInt(i) << BigInt(32)) | BigInt(jump << 1) | BigInt(j);
if (f.has(key)) {
return f.get(key)!;
}

let ans: number = 0;
if (i === k) {
ans++;
}

if (i > 0 && j === 0) {
ans += dfs(i - 1, 1, jump);
}

ans += dfs(i + (1 << jump), 0, jump + 1);
f.set(key, ans);
return ans;
};

return dfs(1, 0, 0);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int waysToReachStair(int k) {
this->k = k;
return dfs(1, 0, 0);
}

private:
unordered_map<long long, int> f;
int k;

int dfs(int i, int j, int jump) {
if (i > k + 1) {
return 0;
}
long long key = ((long long) i << 32) | jump << 1 | j;
if (f.contains(key)) {
return f[key];
}
int ans = i == k ? 1 : 0;
if (i > 0 && j == 0) {
ans += dfs(i - 1, 1, jump);
}
ans += dfs(i + (1 << jump), 0, jump + 1);
f[key] = ans;
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
func waysToReachStair(k int) int {
f := map[int]int{}
var dfs func(i, j, jump int) int
dfs = func(i, j, jump int) int {
if i > k+1 {
return 0
}
key := (i << 32) | jump<<1 | j
if v, has := f[key]; has {
return v
}
ans := 0
if i == k {
ans++
}
if i > 0 && j == 0 {
ans += dfs(i-1, 1, jump)
}
ans += dfs(i+(1<<jump), 0, jump+1)
f[key] = ans
return ans
}
return dfs(1, 0, 0)
}
Loading