Skip to content

Commit 3ec2974

Browse files
committed
solutions: 2140 - Solving Questions With Brainpower (Medium)
1 parent bacf860 commit 3ec2974

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
description: >-
3+
Author: @wkw | https://leetcode.com/problems/solving-questions-with-brainpower/
4+
---
5+
6+
# 2140 - Solving Questions With Brainpower (Medium)
7+
8+
## Problem Link
9+
10+
https://leetcode.com/problems/solving-questions-with-brainpower/
11+
12+
## Problem Statement
13+
14+
You are given a **0-indexed** array `questions` where `questions[i] = [points_i, brainpower_i]`. The questions must be solved in order (i.e., from the first question to the last).
15+
16+
- **If you solve** the `i`-th question:
17+
- You earn `points_i` points.
18+
- You are then forced to **skip** the next `brainpower_i` questions.
19+
- **If you skip** the `i`-th question:
20+
- You move directly to the next question without earning any points.
21+
22+
Your task is to determine the maximum number of points you can earn by either solving or skipping each question optimally.
23+
24+
## Examples
25+
26+
### Example 1
27+
28+
- **Input:** `questions = [[3,2], [4,3], [4,4], [2,5]]`
29+
- **Output:** `5`
30+
- **Explanation:**
31+
- You can solve question `0` to earn `3` points. After solving it, you must skip the next `2` questions (questions `1` and `2`).
32+
- Then, you solve question `3` to earn an additional `2` points.
33+
- In total, you earn `3 + 2 = 5` points, which is optimal.
34+
35+
### Example 2
36+
37+
- **Input:** `questions = [[1,1], [2,2], [3,3], [4,4], [5,5]]`
38+
- **Output:** (Depends on the optimal choices; input here serves as an additional test case.)
39+
- **Explanation:**
40+
- The maximum points depend on intelligently deciding which questions to skip or solve to maximize the overall score.
41+
42+
_Note: Replace or augment the examples with additional ones if needed, based on what the original problem description provides._
43+
44+
## Constraints
45+
46+
- $1 <= questions.length <= 10^5$
47+
- $questions[i].length == 2$
48+
- $1 <= points_i, brainpower_i <= 10^5$
49+
50+
## Approach 1: Dynamic Programming
51+
52+
We can use top-down dynamic programming approach with memoization to solve this problem. We can notice that we have two choices at each step - either skip the current question or solve the current question. If we skip the question, then we simply move to the next one, which is $(i + 1)$. If we solve the $i$-th question, we earn $p$ points and must skip the next $b$ questions. Therefore, we jump to the question at index $i + b + 1$.
53+
54+
For the base case, when the index $i$ goes beyond the last question (i.e. $i >= n$), then return 0 since there are no more question to process.
55+
56+
The complexity for both time & space is $O(n)$.
57+
58+
<SolutionAuthor name="@wkw"/>
59+
60+
```py
61+
# TC: O(n)
62+
# SC: O(n)
63+
class Solution:
64+
def mostPoints(self, questions: List[List[int]]) -> int:
65+
n = len(questions)
66+
@cache
67+
def dfs(i):
68+
# base case
69+
if i >= n: return 0
70+
p, b = questions[i]
71+
# two choices at each step - skip or not skip
72+
# skip - go to next question
73+
# not skip - take the point and skip next b questions
74+
# -> max(skip, not skip)
75+
return max(dfs(i + 1), p + dfs(i + b + 1))
76+
return dfs(0)
77+
```

0 commit comments

Comments
 (0)