|
| 1 | +# 2140. Solving Questions With Brainpower |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Dynamic Programming. |
| 5 | +- Similar Questions: House Robber, Frog Jump. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given a **0-indexed** 2D integer array `questions` where `questions[i] = [pointsi, brainpoweri]`. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | + |
| 14 | + For example, given `questions = [[3, 2], [4, 3], [4, 4], [2, 5]]`: |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +- If question `0` is solved, you will earn `3` points but you will be unable to solve questions `1` and `2`. |
| 19 | + |
| 20 | +- 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`. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +Return **the **maximum** points you can earn for the exam**. |
| 26 | + |
| 27 | + |
| 28 | +Example 1: |
| 29 | + |
| 30 | +``` |
| 31 | +Input: questions = [[3,2],[4,3],[4,4],[2,5]] |
| 32 | +Output: 5 |
| 33 | +Explanation: The maximum points can be earned by solving questions 0 and 3. |
| 34 | +- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions |
| 35 | +- Unable to solve questions 1 and 2 |
| 36 | +- Solve question 3: Earn 2 points |
| 37 | +Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points. |
| 38 | +``` |
| 39 | + |
| 40 | +Example 2: |
| 41 | + |
| 42 | +``` |
| 43 | +Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]] |
| 44 | +Output: 7 |
| 45 | +Explanation: The maximum points can be earned by solving questions 1 and 4. |
| 46 | +- Skip question 0 |
| 47 | +- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions |
| 48 | +- Unable to solve questions 2 and 3 |
| 49 | +- Solve question 4: Earn 5 points |
| 50 | +Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points. |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +**Constraints:** |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +- `1 <= questions.length <= 105` |
| 59 | + |
| 60 | +- `questions[i].length == 2` |
| 61 | + |
| 62 | +- `1 <= pointsi, brainpoweri <= 105` |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## Solution |
| 67 | + |
| 68 | +```javascript |
| 69 | +/** |
| 70 | + * @param {number[][]} questions |
| 71 | + * @return {number} |
| 72 | + */ |
| 73 | +var mostPoints = function(questions) { |
| 74 | + var dp = Array(questions.length); |
| 75 | + for (var i = questions.length - 1; i >= 0; i--) { |
| 76 | + var [points, brainpower] = questions[i]; |
| 77 | + dp[i] = Math.max( |
| 78 | + points + (dp[i + brainpower + 1] || 0), |
| 79 | + dp[i + 1] || 0, |
| 80 | + ); |
| 81 | + } |
| 82 | + return dp[0]; |
| 83 | +}; |
| 84 | +``` |
| 85 | + |
| 86 | +**Explain:** |
| 87 | + |
| 88 | +Dynamic-programming. |
| 89 | + |
| 90 | +**Complexity:** |
| 91 | + |
| 92 | +* Time complexity : O(n). |
| 93 | +* Space complexity : O(n). |
0 commit comments