Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 80f5490

Browse files
committedMar 31, 2025·
feat: add solutions to lc problem: No.2140
No.2140.Solving Questions With Brainpower
1 parent 2a5efa4 commit 80f5490

File tree

5 files changed

+136
-14
lines changed

5 files changed

+136
-14
lines changed
 

‎solution/2100-2199/2140.Solving Questions With Brainpower/README.md

+49-5
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ tags:
7979

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

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

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

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

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

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

@@ -144,7 +144,7 @@ public:
144144
int n = questions.size();
145145
long long f[n];
146146
memset(f, 0, sizeof(f));
147-
function<long long(int)> dfs = [&](int i) -> long long {
147+
auto dfs = [&](this auto&& dfs, int i) -> long long {
148148
if (i >= n) {
149149
return 0;
150150
}
@@ -201,6 +201,32 @@ function mostPoints(questions: number[][]): number {
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
209+
let n = questions.len();
210+
let mut f = vec![-1; n];
211+
212+
fn dfs(i: usize, questions: &Vec<Vec<i32>>, f: &mut Vec<i64>) -> i64 {
213+
if i >= questions.len() {
214+
return 0;
215+
}
216+
if f[i] != -1 {
217+
return f[i];
218+
}
219+
let p = questions[i][0] as i64;
220+
let b = questions[i][1] as usize;
221+
f[i] = (p + dfs(i + b + 1, questions, f)).max(dfs(i + 1, questions, f));
222+
f[i]
223+
}
224+
225+
dfs(0, &questions, &mut f)
226+
}
227+
}
228+
```
229+
204230
<!-- tabs:end -->
205231

206232
<!-- solution:end -->
@@ -305,6 +331,24 @@ function mostPoints(questions: number[][]): number {
305331
}
306332
```
307333

334+
#### Rust
335+
336+
```rust
337+
impl Solution {
338+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
339+
let n = questions.len();
340+
let mut f = vec![0; n + 1];
341+
for i in (0..n).rev() {
342+
let p = questions[i][0] as i64;
343+
let b = questions[i][1] as usize;
344+
let j = i + b + 1;
345+
f[i] = f[i + 1].max(p + if j > n { 0 } else { f[j] });
346+
}
347+
f[0]
348+
}
349+
}
350+
```
351+
308352
<!-- tabs:end -->
309353

310354
<!-- solution:end -->

‎solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md

+52-8
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1: Memoization Search
80+
### Solution 1: Memoization
8181

82-
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)$.
82+
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)$.
8383

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

86-
- If $i \geq n$, it means that all problems have been solved, return $0$;
87-
- 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))$.
86+
- If $i \geq n$, it means all questions have been solved, so return $0$;
87+
- 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))$.
8888

89-
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)$.
89+
To avoid repeated calculations, we can use memoization by storing the values of $\textit{dfs}(i)$ in an array $f$.
9090

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

9393
<!-- tabs:start -->
9494

@@ -144,7 +144,7 @@ public:
144144
int n = questions.size();
145145
long long f[n];
146146
memset(f, 0, sizeof(f));
147-
function<long long(int)> dfs = [&](int i) -> long long {
147+
auto dfs = [&](this auto&& dfs, int i) -> long long {
148148
if (i >= n) {
149149
return 0;
150150
}
@@ -201,6 +201,32 @@ function mostPoints(questions: number[][]): number {
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
209+
let n = questions.len();
210+
let mut f = vec![-1; n];
211+
212+
fn dfs(i: usize, questions: &Vec<Vec<i32>>, f: &mut Vec<i64>) -> i64 {
213+
if i >= questions.len() {
214+
return 0;
215+
}
216+
if f[i] != -1 {
217+
return f[i];
218+
}
219+
let p = questions[i][0] as i64;
220+
let b = questions[i][1] as usize;
221+
f[i] = (p + dfs(i + b + 1, questions, f)).max(dfs(i + 1, questions, f));
222+
f[i]
223+
}
224+
225+
dfs(0, &questions, &mut f)
226+
}
227+
}
228+
```
229+
204230
<!-- tabs:end -->
205231

206232
<!-- solution:end -->
@@ -305,6 +331,24 @@ function mostPoints(questions: number[][]): number {
305331
}
306332
```
307333

334+
#### Rust
335+
336+
```rust
337+
impl Solution {
338+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
339+
let n = questions.len();
340+
let mut f = vec![0; n + 1];
341+
for i in (0..n).rev() {
342+
let p = questions[i][0] as i64;
343+
let b = questions[i][1] as usize;
344+
let j = i + b + 1;
345+
f[i] = f[i + 1].max(p + if j > n { 0 } else { f[j] });
346+
}
347+
f[0]
348+
}
349+
}
350+
```
351+
308352
<!-- tabs:end -->
309353

310354
<!-- solution:end -->

‎solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Solution {
44
int n = questions.size();
55
long long f[n];
66
memset(f, 0, sizeof(f));
7-
function<long long(int)> dfs = [&](int i) -> long long {
7+
auto dfs = [&](this auto&& dfs, int i) -> long long {
88
if (i >= n) {
99
return 0;
1010
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
3+
let n = questions.len();
4+
let mut f = vec![-1; n];
5+
6+
fn dfs(i: usize, questions: &Vec<Vec<i32>>, f: &mut Vec<i64>) -> i64 {
7+
if i >= questions.len() {
8+
return 0;
9+
}
10+
if f[i] != -1 {
11+
return f[i];
12+
}
13+
let p = questions[i][0] as i64;
14+
let b = questions[i][1] as usize;
15+
f[i] = (p + dfs(i + b + 1, questions, f)).max(dfs(i + 1, questions, f));
16+
f[i]
17+
}
18+
19+
dfs(0, &questions, &mut f)
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
3+
let n = questions.len();
4+
let mut f = vec![0; n + 1];
5+
for i in (0..n).rev() {
6+
let p = questions[i][0] as i64;
7+
let b = questions[i][1] as usize;
8+
let j = i + b + 1;
9+
f[i] = f[i + 1].max(p + if j > n { 0 } else { f[j] });
10+
}
11+
f[0]
12+
}
13+
}

0 commit comments

Comments
 (0)