Skip to content
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

feat: add solutions to lc problem: No.2140 #4316

Merged
merged 1 commit into from
Mar 31, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ tags:

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

我们设计一个函数 $dfs(i)$,表示从第 $i$ 个问题开始解决,能够获得的最高分数。那么答案就是 $dfs(0)$。
我们设计一个函数 $\textit{dfs}(i)$,表示从第 $i$ 个问题开始解决,能够获得的最高分数。那么答案就是 $\textit{dfs}(0)$。

函数 $dfs(i)$ 的计算方式如下:
函数 $\textit{dfs}(i)$ 的计算方式如下:

- 如果 $i \geq n$,表示已经解决完所有问题,返回 $0$;
- 否则,设第 $i$ 个问题的分数为 $p$,需要跳过的问题数为 $b$,那么 $dfs(i) = \max(p + dfs(i + b + 1), dfs(i + 1))$。
- 否则,设第 $i$ 个问题的分数为 $p$,需要跳过的问题数为 $b$,那么 $\textit{dfs}(i) = \max(p + \textit{dfs}(i + b + 1), \textit{dfs}(i + 1))$。

为了避免重复计算,我们可以使用记忆化搜索的方法,用一个数组 $f$ 记录所有已经计算过的 $dfs(i)$ 的值。
为了避免重复计算,我们可以使用记忆化搜索的方法,用一个数组 $f$ 记录所有已经计算过的 $\textit{dfs}(i)$ 的值。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是问题的数量。

Expand Down Expand Up @@ -144,7 +144,7 @@ public:
int n = questions.size();
long long f[n];
memset(f, 0, sizeof(f));
function<long long(int)> dfs = [&](int i) -> long long {
auto dfs = [&](this auto&& dfs, int i) -> long long {
if (i >= n) {
return 0;
}
Expand Down Expand Up @@ -201,6 +201,32 @@ function mostPoints(questions: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut f = vec![-1; n];

fn dfs(i: usize, questions: &Vec<Vec<i32>>, f: &mut Vec<i64>) -> i64 {
if i >= questions.len() {
return 0;
}
if f[i] != -1 {
return f[i];
}
let p = questions[i][0] as i64;
let b = questions[i][1] as usize;
f[i] = (p + dfs(i + b + 1, questions, f)).max(dfs(i + 1, questions, f));
f[i]
}

dfs(0, &questions, &mut f)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -305,6 +331,24 @@ function mostPoints(questions: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut f = vec![0; n + 1];
for i in (0..n).rev() {
let p = questions[i][0] as i64;
let b = questions[i][1] as usize;
let j = i + b + 1;
f[i] = f[i + 1].max(p + if j > n { 0 } else { f[j] });
}
f[0]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.

<!-- solution:start -->

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

We design a function $dfs(i)$, which represents the maximum score that can be obtained starting from the $i$-th problem. Therefore, the answer is $dfs(0)$.
We design a function $\textit{dfs}(i)$, which represents the maximum score that can be obtained starting from the $i$-th question. The answer is $\textit{dfs}(0)$.

The calculation method of the function $dfs(i)$ is as follows:
The function $\textit{dfs}(i)$ is calculated as follows:

- If $i \geq n$, it means that all problems have been solved, return $0$;
- Otherwise, let the score of the $i$-th problem be $p$, and the number of problems to skip be $b$, then $dfs(i) = \max(p + dfs(i + b + 1), dfs(i + 1))$.
- If $i \geq n$, it means all questions have been solved, so return $0$;
- Otherwise, let the score of the $i$-th question be $p$, and the number of questions to skip be $b$. Then, $\textit{dfs}(i) = \max(p + \textit{dfs}(i + b + 1), \textit{dfs}(i + 1))$.

To avoid repeated calculations, we can use the method of memoization search, using an array $f$ to record the values of all already computed $dfs(i)$.
To avoid repeated calculations, we can use memoization by storing the values of $\textit{dfs}(i)$ in an array $f$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of problems.
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of questions.

<!-- tabs:start -->

Expand Down Expand Up @@ -144,7 +144,7 @@ public:
int n = questions.size();
long long f[n];
memset(f, 0, sizeof(f));
function<long long(int)> dfs = [&](int i) -> long long {
auto dfs = [&](this auto&& dfs, int i) -> long long {
if (i >= n) {
return 0;
}
Expand Down Expand Up @@ -201,6 +201,32 @@ function mostPoints(questions: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut f = vec![-1; n];

fn dfs(i: usize, questions: &Vec<Vec<i32>>, f: &mut Vec<i64>) -> i64 {
if i >= questions.len() {
return 0;
}
if f[i] != -1 {
return f[i];
}
let p = questions[i][0] as i64;
let b = questions[i][1] as usize;
f[i] = (p + dfs(i + b + 1, questions, f)).max(dfs(i + 1, questions, f));
f[i]
}

dfs(0, &questions, &mut f)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -305,6 +331,24 @@ function mostPoints(questions: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut f = vec![0; n + 1];
for i in (0..n).rev() {
let p = questions[i][0] as i64;
let b = questions[i][1] as usize;
let j = i + b + 1;
f[i] = f[i + 1].max(p + if j > n { 0 } else { f[j] });
}
f[0]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Solution {
int n = questions.size();
long long f[n];
memset(f, 0, sizeof(f));
function<long long(int)> dfs = [&](int i) -> long long {
auto dfs = [&](this auto&& dfs, int i) -> long long {
if (i >= n) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut f = vec![-1; n];

fn dfs(i: usize, questions: &Vec<Vec<i32>>, f: &mut Vec<i64>) -> i64 {
if i >= questions.len() {
return 0;
}
if f[i] != -1 {
return f[i];
}
let p = questions[i][0] as i64;
let b = questions[i][1] as usize;
f[i] = (p + dfs(i + b + 1, questions, f)).max(dfs(i + 1, questions, f));
f[i]
}

dfs(0, &questions, &mut f)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut f = vec![0; n + 1];
for i in (0..n).rev() {
let p = questions[i][0] as i64;
let b = questions[i][1] as usize;
let j = i + b + 1;
f[i] = f[i + 1].max(p + if j > n { 0 } else { f[j] });
}
f[0]
}
}
Loading