Skip to content

Commit b10e32e

Browse files
committed
+ problem 2140
1 parent e0566b6 commit b10e32e

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 2140. Solving Questions With Brainpower
2+
You are given a **0-indexed** 2D integer array `questions` where <code>questions[i] = [points<sub>i</sub>, brainpower<sub>i</sub>]</code>.
3+
4+
The array describes the questions of an exam, where you have to process the questions **in order** (i.e., starting from question `0`) and make a decision whether to **solve** or **skip** each question. Solving question `i` will **earn** you <code>points<sub>i</sub></code> points but you will be **unable** to solve each of the next <code>brainpower<sub>i</sub></code> questions. If you skip question `i`, you get to make the decision on the next question.
5+
6+
* For example, given `questions = [[3, 2], [4, 3], [4, 4], [2, 5]]`:
7+
* If question `0` is solved, you will earn `3` points but you will be unable to solve questions `1` and `2`.
8+
* If instead, question `0` is skipped and question `1` is solved, you will earn `4` points but you will be unable to solve questions `2` and `3`.
9+
10+
Return *the **maximum** points you can earn for the exam*.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> questions = [[3,2],[4,3],[4,4],[2,5]]
15+
<strong>Output:</strong> 5
16+
<strong>Explanation:</strong> The maximum points can be earned by solving questions 0 and 3.
17+
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
18+
- Unable to solve questions 1 and 2
19+
- Solve question 3: Earn 2 points
20+
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
21+
</pre>
22+
23+
#### Example 2:
24+
<pre>
25+
<strong>Input:</strong> questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
26+
<strong>Output:</strong> 7
27+
<strong>Explanation:</strong> The maximum points can be earned by solving questions 1 and 4.
28+
- Skip question 0
29+
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
30+
- Unable to solve questions 2 and 3
31+
- Solve question 4: Earn 5 points
32+
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
33+
</pre>
34+
35+
#### Constraints:
36+
* <code>1 <= questions.length <= 10<sup>5</sup></code>
37+
* `questions[i].length == 2`
38+
* <code>1 <= points<sub>i</sub>, brainpower<sub>i</sub> <= 10<sup>5</sup></code>
39+
40+
## Solutions (Rust)
41+
42+
### 1. Solution
43+
```Rust
44+
impl Solution {
45+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
46+
let mut dp = vec![(0, 0); questions.len()];
47+
48+
for i in (0..dp.len()).rev() {
49+
let (points, brainpower) = (questions[i][0] as i64, questions[i][1] as usize);
50+
51+
dp[i].0 = points;
52+
if i + brainpower + 1 < dp.len() {
53+
dp[i].0 += dp[i + brainpower + 1].0.max(dp[i + brainpower + 1].1);
54+
}
55+
if i + 1 < dp.len() {
56+
dp[i].1 = dp[i + 1].0.max(dp[i + 1].1);
57+
}
58+
}
59+
60+
dp[0].0.max(dp[0].1)
61+
}
62+
}
63+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 2140. 解决智力问题
2+
给你一个下标从 **0** 开始的二维整数数组 `questions` ,其中 <code>questions[i] = [points<sub>i</sub>, brainpower<sub>i</sub>]</code> 。
3+
4+
这个数组表示一场考试里的一系列题目,你需要 **按顺序** (也就是从问题 `0` 开始依次解决),针对每个问题选择 **解决** 或者 **跳过** 操作。解决问题 `i` 将让你 **获得** <code>points<sub>i</sub></code> 的分数,但是你将 **无法** 解决接下来的 <code>brainpower<sub>i</sub></code> 个问题(即只能跳过接下来的 <code>brainpower<sub>i</sub></code> 个问题)。如果你跳过问题 `i` ,你可以对下一个问题决定使用哪种操作。
5+
6+
* 比方说,给你 `questions = [[3, 2], [4, 3], [4, 4], [2, 5]]`
7+
* 如果问题 `0` 被解决了, 那么你可以获得 `3` 分,但你不能解决问题 `1``2`
8+
* 如果你跳过问题 `0` ,且解决问题 `1` ,你将获得 `4` 分但是不能解决问题 `2``3`
9+
10+
请你返回这场考试里你能获得的 **最高** 分数。
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> questions = [[3,2],[4,3],[4,4],[2,5]]
15+
<strong>输出:</strong> 5
16+
<strong>解释:</strong> 解决问题 0 和 3 得到最高分。
17+
- 解决问题 0 :获得 3 分,但接下来 2 个问题都不能解决。
18+
- 不能解决问题 1 和 2
19+
- 解决问题 3 :获得 2 分
20+
总得分为:3 + 2 = 5 。没有别的办法获得 5 分或者多于 5 分。
21+
</pre>
22+
23+
#### 示例 2:
24+
<pre>
25+
<strong>输入:</strong> questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
26+
<strong>输出:</strong> 7
27+
<strong>解释:</strong> 解决问题 1 和 4 得到最高分。
28+
- 跳过问题 0
29+
- 解决问题 1 :获得 2 分,但接下来 2 个问题都不能解决。
30+
- 不能解决问题 2 和 3
31+
- 解决问题 4 :获得 5 分
32+
总得分为:2 + 5 = 7 。没有别的办法获得 7 分或者多于 7 分。
33+
</pre>
34+
35+
#### 提示:
36+
* <code>1 <= questions.length <= 10<sup>5</sup></code>
37+
* `questions[i].length == 2`
38+
* <code>1 <= points<sub>i</sub>, brainpower<sub>i</sub> <= 10<sup>5</sup></code>
39+
40+
## 题解 (Rust)
41+
42+
### 1. 题解
43+
```Rust
44+
impl Solution {
45+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
46+
let mut dp = vec![(0, 0); questions.len()];
47+
48+
for i in (0..dp.len()).rev() {
49+
let (points, brainpower) = (questions[i][0] as i64, questions[i][1] as usize);
50+
51+
dp[i].0 = points;
52+
if i + brainpower + 1 < dp.len() {
53+
dp[i].0 += dp[i + brainpower + 1].0.max(dp[i + brainpower + 1].1);
54+
}
55+
if i + 1 < dp.len() {
56+
dp[i].1 = dp[i + 1].0.max(dp[i + 1].1);
57+
}
58+
}
59+
60+
dp[0].0.max(dp[0].1)
61+
}
62+
}
63+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
3+
let mut dp = vec![(0, 0); questions.len()];
4+
5+
for i in (0..dp.len()).rev() {
6+
let (points, brainpower) = (questions[i][0] as i64, questions[i][1] as usize);
7+
8+
dp[i].0 = points;
9+
if i + brainpower + 1 < dp.len() {
10+
dp[i].0 += dp[i + brainpower + 1].0.max(dp[i + brainpower + 1].1);
11+
}
12+
if i + 1 < dp.len() {
13+
dp[i].1 = dp[i + 1].0.max(dp[i + 1].1);
14+
}
15+
}
16+
17+
dp[0].0.max(dp[0].1)
18+
}
19+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@
11061106
[2134][2134l]|[Minimum Swaps to Group All 1's Together II][2134] |![rs]
11071107
[2138][2138l]|[Divide a String Into Groups of Size k][2138] |![py]
11081108
[2139][2139l]|[Minimum Moves to Reach Target Score][2139] |![rs]
1109+
[2140][2140l]|[Solving Questions With Brainpower][2140] |![rs]
11091110
[2144][2144l]|[Minimum Cost of Buying Candies With Discount][2144] |![rs]
11101111
[2145][2145l]|[Count the Hidden Sequences][2145] |![rs]
11111112
[2147][2147l]|[Number of Ways to Divide a Long Corridor][2147] |![rs]
@@ -2424,6 +2425,7 @@
24242425
[2134]:Problemset/2134-Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/README.md#2134-minimum-swaps-to-group-all-1s-together-ii
24252426
[2138]:Problemset/2138-Divide%20a%20String%20Into%20Groups%20of%20Size%20k/README.md#2138-divide-a-string-into-groups-of-size-k
24262427
[2139]:Problemset/2139-Minimum%20Moves%20to%20Reach%20Target%20Score/README.md#2139-minimum-moves-to-reach-target-score
2428+
[2140]:Problemset/2140-Solving%20Questions%20With%20Brainpower/README.md#2140-solving-questions-with-brainpower
24272429
[2144]:Problemset/2144-Minimum%20Cost%20of%20Buying%20Candies%20With%20Discount/README.md#2144-minimum-cost-of-buying-candies-with-discount
24282430
[2145]:Problemset/2145-Count%20the%20Hidden%20Sequences/README.md#2145-count-the-hidden-sequences
24292431
[2147]:Problemset/2147-Number%20of%20Ways%20to%20Divide%20a%20Long%20Corridor/README.md#2147-number-of-ways-to-divide-a-long-corridor
@@ -3745,6 +3747,7 @@
37453747
[2134l]:https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/
37463748
[2138l]:https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/
37473749
[2139l]:https://leetcode.com/problems/minimum-moves-to-reach-target-score/
3750+
[2140l]:https://leetcode.com/problems/solving-questions-with-brainpower/
37483751
[2144l]:https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/
37493752
[2145l]:https://leetcode.com/problems/count-the-hidden-sequences/
37503753
[2147l]:https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@
11061106
[2134][2134l]|[最少交换次数来组合所有的 1 II][2134] |![rs]
11071107
[2138][2138l]|[将字符串拆分为若干长度为 k 的组][2138] |![py]
11081108
[2139][2139l]|[得到目标值的最少行动次数][2139] |![rs]
1109+
[2140][2140l]|[解决智力问题][2140] |![rs]
11091110
[2144][2144l]|[打折购买糖果的最小开销][2144] |![rs]
11101111
[2145][2145l]|[统计隐藏数组数目][2145] |![rs]
11111112
[2147][2147l]|[分隔长廊的方案数][2147] |![rs]
@@ -2424,6 +2425,7 @@
24242425
[2134]:Problemset/2134-Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/README_CN.md#2134-最少交换次数来组合所有的-1-ii
24252426
[2138]:Problemset/2138-Divide%20a%20String%20Into%20Groups%20of%20Size%20k/README_CN.md#2138-将字符串拆分为若干长度为-k-的组
24262427
[2139]:Problemset/2139-Minimum%20Moves%20to%20Reach%20Target%20Score/README_CN.md#2139-得到目标值的最少行动次数
2428+
[2140]:Problemset/2140-Solving%20Questions%20With%20Brainpower/README_CN.md#2140-解决智力问题
24272429
[2144]:Problemset/2144-Minimum%20Cost%20of%20Buying%20Candies%20With%20Discount/README_CN.md#2144-打折购买糖果的最小开销
24282430
[2145]:Problemset/2145-Count%20the%20Hidden%20Sequences/README_CN.md#2145-统计隐藏数组数目
24292431
[2147]:Problemset/2147-Number%20of%20Ways%20to%20Divide%20a%20Long%20Corridor/README_CN.md#2147-分隔长廊的方案数
@@ -3745,6 +3747,7 @@
37453747
[2134l]:https://leetcode.cn/problems/minimum-swaps-to-group-all-1s-together-ii/
37463748
[2138l]:https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/
37473749
[2139l]:https://leetcode.cn/problems/minimum-moves-to-reach-target-score/
3750+
[2140l]:https://leetcode.cn/problems/solving-questions-with-brainpower/
37483751
[2144l]:https://leetcode.cn/problems/minimum-cost-of-buying-candies-with-discount/
37493752
[2145l]:https://leetcode.cn/problems/count-the-hidden-sequences/
37503753
[2147l]:https://leetcode.cn/problems/number-of-ways-to-divide-a-long-corridor/

0 commit comments

Comments
 (0)