diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/README.md b/solution/2100-2199/2140.Solving Questions With Brainpower/README.md index b0040edf5e294..7dffd4004153c 100644 --- a/solution/2100-2199/2140.Solving Questions With Brainpower/README.md +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/README.md @@ -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$ 是问题的数量。 @@ -144,7 +144,7 @@ public: int n = questions.size(); long long f[n]; memset(f, 0, sizeof(f)); - function dfs = [&](int i) -> long long { + auto dfs = [&](this auto&& dfs, int i) -> long long { if (i >= n) { return 0; } @@ -201,6 +201,32 @@ function mostPoints(questions: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn most_points(questions: Vec>) -> i64 { + let n = questions.len(); + let mut f = vec![-1; n]; + + fn dfs(i: usize, questions: &Vec>, f: &mut Vec) -> 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) + } +} +``` + @@ -305,6 +331,24 @@ function mostPoints(questions: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn most_points(questions: Vec>) -> 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] + } +} +``` + diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md b/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md index fc240c1ce5ef0..0b7d21ade7f48 100644 --- a/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md @@ -77,18 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points. -### 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. @@ -144,7 +144,7 @@ public: int n = questions.size(); long long f[n]; memset(f, 0, sizeof(f)); - function dfs = [&](int i) -> long long { + auto dfs = [&](this auto&& dfs, int i) -> long long { if (i >= n) { return 0; } @@ -201,6 +201,32 @@ function mostPoints(questions: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn most_points(questions: Vec>) -> i64 { + let n = questions.len(); + let mut f = vec![-1; n]; + + fn dfs(i: usize, questions: &Vec>, f: &mut Vec) -> 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) + } +} +``` + @@ -305,6 +331,24 @@ function mostPoints(questions: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn most_points(questions: Vec>) -> 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] + } +} +``` + diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp index a6a0f5001f673..141080c88aaf7 100644 --- a/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp @@ -4,7 +4,7 @@ class Solution { int n = questions.size(); long long f[n]; memset(f, 0, sizeof(f)); - function dfs = [&](int i) -> long long { + auto dfs = [&](this auto&& dfs, int i) -> long long { if (i >= n) { return 0; } diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.rs b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.rs new file mode 100644 index 0000000000000..04565bcf507d2 --- /dev/null +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn most_points(questions: Vec>) -> i64 { + let n = questions.len(); + let mut f = vec![-1; n]; + + fn dfs(i: usize, questions: &Vec>, f: &mut Vec) -> 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) + } +} diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/Solution2.rs b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution2.rs new file mode 100644 index 0000000000000..4e348b165a196 --- /dev/null +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution2.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn most_points(questions: Vec>) -> 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] + } +}