Skip to content

Commit 3ba274a

Browse files
committed
feat: add solutions to lc problem: No.2140
No.2140.Solving Questions With Brainpower
1 parent 7efca83 commit 3ba274a

File tree

6 files changed

+242
-2
lines changed

6 files changed

+242
-2
lines changed

solution/2100-2199/2140.Solving Questions With Brainpower/README.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,106 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:记忆化搜索**
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
<!-- 这里可写当前语言的特殊实现逻辑 -->
6870

6971
```python
70-
72+
class Solution:
73+
def mostPoints(self, questions: List[List[int]]) -> int:
74+
@cache
75+
def dfs(i):
76+
if i >= len(questions):
77+
return 0
78+
return max(questions[i][0] + dfs(i + questions[i][1] + 1), dfs(i + 1))
79+
80+
return dfs(0)
7181
```
7282

7383
### **Java**
7484

7585
<!-- 这里可写当前语言的特殊实现逻辑 -->
7686

7787
```java
88+
class Solution {
89+
private long[] memo;
90+
private int[][] questions;
91+
92+
public long mostPoints(int[][] questions) {
93+
this.questions = questions;
94+
memo = new long[questions.length];
95+
Arrays.fill(memo, -1);
96+
return dfs(0);
97+
}
98+
99+
private long dfs(int i) {
100+
if (i >= questions.length) {
101+
return 0;
102+
}
103+
if (memo[i] != -1) {
104+
return memo[i];
105+
}
106+
long ans = Math.max(questions[i][0] + dfs(i + questions[i][1] + 1), dfs(i + 1));
107+
memo[i] = ans;
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
long long mostPoints(vector<vector<int>>& questions) {
119+
vector<long long> memo(questions.size(), -1);
120+
return dfs(0, questions, memo);
121+
}
122+
123+
long long dfs(int i, vector<vector<int>>& questions, vector<long long>& memo) {
124+
if (i >= questions.size()) return 0;
125+
if (memo[i] != -1) return memo[i];
126+
long long ans = max(questions[i][0] + dfs(i + questions[i][1] + 1, questions, memo), dfs(i + 1, questions, memo));
127+
memo[i] = ans;
128+
return ans;
129+
}
130+
};
131+
```
78132

133+
### **Go**
134+
135+
```go
136+
func mostPoints(questions [][]int) int64 {
137+
n := len(questions)
138+
memo := make([]int, n)
139+
for i := range memo {
140+
memo[i] = -1
141+
}
142+
var dfs func(i int) int
143+
dfs = func(i int) int {
144+
if i >= n {
145+
return 0
146+
}
147+
if memo[i] != -1 {
148+
return memo[i]
149+
}
150+
ans := max(questions[i][0]+dfs(i+questions[i][1]+1), dfs(i+1))
151+
memo[i] = ans
152+
return ans
153+
}
154+
return int64(dfs(0))
155+
}
156+
157+
func max(a, b int) int {
158+
if a > b {
159+
return a
160+
}
161+
return b
162+
}
79163
```
80164

81165
### **TypeScript**

solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,95 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def mostPoints(self, questions: List[List[int]]) -> int:
66+
@cache
67+
def dfs(i):
68+
if i >= len(questions):
69+
return 0
70+
return max(questions[i][0] + dfs(i + questions[i][1] + 1), dfs(i + 1))
71+
72+
return dfs(0)
6573
```
6674

6775
### **Java**
6876

6977
```java
78+
class Solution {
79+
private long[] memo;
80+
private int[][] questions;
81+
82+
public long mostPoints(int[][] questions) {
83+
this.questions = questions;
84+
memo = new long[questions.length];
85+
Arrays.fill(memo, -1);
86+
return dfs(0);
87+
}
88+
89+
private long dfs(int i) {
90+
if (i >= questions.length) {
91+
return 0;
92+
}
93+
if (memo[i] != -1) {
94+
return memo[i];
95+
}
96+
long ans = Math.max(questions[i][0] + dfs(i + questions[i][1] + 1), dfs(i + 1));
97+
memo[i] = ans;
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
long long mostPoints(vector<vector<int>>& questions) {
109+
vector<long long> memo(questions.size(), -1);
110+
return dfs(0, questions, memo);
111+
}
112+
113+
long long dfs(int i, vector<vector<int>>& questions, vector<long long>& memo) {
114+
if (i >= questions.size()) return 0;
115+
if (memo[i] != -1) return memo[i];
116+
long long ans = max(questions[i][0] + dfs(i + questions[i][1] + 1, questions, memo), dfs(i + 1, questions, memo));
117+
memo[i] = ans;
118+
return ans;
119+
}
120+
};
121+
```
70122

123+
### **Go**
124+
125+
```go
126+
func mostPoints(questions [][]int) int64 {
127+
n := len(questions)
128+
memo := make([]int, n)
129+
for i := range memo {
130+
memo[i] = -1
131+
}
132+
var dfs func(i int) int
133+
dfs = func(i int) int {
134+
if i >= n {
135+
return 0
136+
}
137+
if memo[i] != -1 {
138+
return memo[i]
139+
}
140+
ans := max(questions[i][0]+dfs(i+questions[i][1]+1), dfs(i+1))
141+
memo[i] = ans
142+
return ans
143+
}
144+
return int64(dfs(0))
145+
}
146+
147+
func max(a, b int) int {
148+
if a > b {
149+
return a
150+
}
151+
return b
152+
}
71153
```
72154

73155
### **TypeScript**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
long long mostPoints(vector<vector<int>>& questions) {
4+
vector<long long> memo(questions.size(), -1);
5+
return dfs(0, questions, memo);
6+
}
7+
8+
long long dfs(int i, vector<vector<int>>& questions, vector<long long>& memo) {
9+
if (i >= questions.size()) return 0;
10+
if (memo[i] != -1) return memo[i];
11+
long long ans = max(questions[i][0] + dfs(i + questions[i][1] + 1, questions, memo), dfs(i + 1, questions, memo));
12+
memo[i] = ans;
13+
return ans;
14+
}
15+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func mostPoints(questions [][]int) int64 {
2+
n := len(questions)
3+
memo := make([]int, n)
4+
for i := range memo {
5+
memo[i] = -1
6+
}
7+
var dfs func(i int) int
8+
dfs = func(i int) int {
9+
if i >= n {
10+
return 0
11+
}
12+
if memo[i] != -1 {
13+
return memo[i]
14+
}
15+
ans := max(questions[i][0]+dfs(i+questions[i][1]+1), dfs(i+1))
16+
memo[i] = ans
17+
return ans
18+
}
19+
return int64(dfs(0))
20+
}
21+
22+
func max(a, b int) int {
23+
if a > b {
24+
return a
25+
}
26+
return b
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
private long[] memo;
3+
private int[][] questions;
4+
5+
public long mostPoints(int[][] questions) {
6+
this.questions = questions;
7+
memo = new long[questions.length];
8+
Arrays.fill(memo, -1);
9+
return dfs(0);
10+
}
11+
12+
private long dfs(int i) {
13+
if (i >= questions.length) {
14+
return 0;
15+
}
16+
if (memo[i] != -1) {
17+
return memo[i];
18+
}
19+
long ans = Math.max(questions[i][0] + dfs(i + questions[i][1] + 1), dfs(i + 1));
20+
memo[i] = ans;
21+
return ans;
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def mostPoints(self, questions: List[List[int]]) -> int:
3+
@cache
4+
def dfs(i):
5+
if i >= len(questions):
6+
return 0
7+
return max(questions[i][0] + dfs(i + questions[i][1] + 1), dfs(i + 1))
8+
9+
return dfs(0)

0 commit comments

Comments
 (0)