[LeetCode] 2140. Solving Questions With Brainpower

You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri].

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 pointsi points but you will be unable to solve each of the next brainpoweri questions. If you skip question i, you get to make the decision on the next question.

  • For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:
    • If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.
    • 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.

Return the maximum points you can earn for the exam.

Example 1:

Input: questions = [[3,2],[4,3],[4,4],[2,5]]
Output: 5
Explanation: The maximum points can be earned by solving questions 0 and 3.
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
- Unable to solve questions 1 and 2
- Solve question 3: Earn 2 points
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.

Example 2:

Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
Output: 7
Explanation: The maximum points can be earned by solving questions 1 and 4.
- Skip question 0
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
- Unable to solve questions 2 and 3
- Solve question 4: Earn 5 points
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.

Constraints:

  • 1 <= questions.length <= 105
  • questions[i].length == 2
  • 1 <= pointsi, brainpoweri <= 105

解决智力问题。

给你一个下标从 0 开始的二维整数数组 questions ,其中 questions[i] = [pointsi, brainpoweri] 。

这个数组表示一场考试里的一系列题目,你需要 按顺序 (也就是从问题 0 开始依次解决),针对每个问题选择 解决 或者 跳过 操作。解决问题 i 将让你 获得  pointsi 的分数,但是你将 无法 解决接下来的 brainpoweri 个问题(即只能跳过接下来的 brainpoweri 个问题)。如果你跳过问题 i ,你可以对下一个问题决定使用哪种操作。

比方说,给你 questions = [[3, 2], [4, 3], [4, 4], [2, 5]] :
如果问题 0 被解决了, 那么你可以获得 3 分,但你不能解决问题 1 和 2 。
如果你跳过问题 0 ,且解决问题 1 ,你将获得 4 分但是不能解决问题 2 和 3 。
请你返回这场考试里你能获得的 最高 分数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/solving-questions-with-brainpower
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是 DP,我参考了这个帖子。这道题可以从左往右计算,也可以从右往左计算。我这里分享一个从左往右计算的做法,因为比较好思考。这里 dp[i] 的定义是从第 0 个问题到第 I - 1 个问题

对于在 index == i 位置上的问题,questions[i][0] 是得分,questions[i][0] 是需要跳过的问题的个数,所以对于位置 i 来说,

如果不解决这个问题,那么在当前位置的得分 = 上一个位置的得分,即 dp[i] = dp[i - 1]

如果解决这个问题,因为涉及到需要跳过的问题,所以我们直接看他跳到的下一个位置 j 的得分是多少,其中 j = i + 1 + questions[i][0],如果是从位置 i 跳过来的,他的得分 dp[j] = dp[i] + questions[i][0]

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public long mostPoints(int[][] questions) {
 3         var n = questions.length;
 4         var f = new long[n + 1];
 5         for (var i = 0; i < n; i++) {
 6             f[i + 1] = Math.max(f[i + 1], f[i]);
 7             var q = questions[i];
 8             var j = Math.min(i + q[1] + 1, n);
 9             f[j] = Math.max(f[j], f[i] + q[0]);
10         }
11         return f[n];
12     }
13 }
14 // 正序

 

LeetCode 题目总结

posted @ 2023-05-13 05:41  CNoodle  阅读(49)  评论(0编辑  收藏  举报