LeetCode 2140.解决智力问题:记忆化搜索(DFS) / 动态规划(DP)
【LetMeFly】2140.解决智力问题:记忆化搜索(DFS) / 动态规划(DP)
力扣题目链接:https://leetcode.cn/problems/solving-questions-with-brainpower/
给你一个下标从 0 开始的二维整数数组 questions
,其中 questions[i] = [pointsi, brainpoweri]
。
这个数组表示一场考试里的一系列题目,你需要 按顺序 (也就是从问题 0
开始依次解决),针对每个问题选择 解决 或者 跳过 操作。解决问题 i
将让你 获得 pointsi
的分数,但是你将 无法 解决接下来的 brainpoweri
个问题(即只能跳过接下来的 brainpoweri
个问题)。如果你跳过问题 i
,你可以对下一个问题决定使用哪种操作。
- 比方说,给你
questions = [[3, 2], [4, 3], [4, 4], [2, 5]]
:<ul> <li>如果问题 <code>0</code> 被解决了, 那么你可以获得 <code>3</code> 分,但你不能解决问题 <code>1</code> 和 <code>2</code> 。</li> <li>如果你跳过问题 <code>0</code> ,且解决问题 <code>1</code> ,你将获得 <code>4</code> 分但是不能解决问题 <code>2</code> 和 <code>3</code> 。</li> </ul> </li>
请你返回这场考试里你能获得的 最高 分数。
示例 1:
输入:questions = [[3,2],[4,3],[4,4],[2,5]] 输出:5 解释:解决问题 0 和 3 得到最高分。 - 解决问题 0 :获得 3 分,但接下来 2 个问题都不能解决。 - 不能解决问题 1 和 2 - 解决问题 3 :获得 2 分 总得分为:3 + 2 = 5 。没有别的办法获得 5 分或者多于 5 分。
示例 2:
输入:questions = [[1,1],[2,2],[3,3],[4,4],[5,5]] 输出:7 解释:解决问题 1 和 4 得到最高分。 - 跳过问题 0 - 解决问题 1 :获得 2 分,但接下来 2 个问题都不能解决。 - 不能解决问题 2 和 3 - 解决问题 4 :获得 5 分 总得分为:2 + 5 = 7 。没有别的办法获得 7 分或者多于 7 分。
提示:
1 <= questions.length <= 105
questions[i].length == 2
1 <= pointsi, brainpoweri <= 105
解题方法一:记忆化搜索
对于问题i
:
- 不选
i
则有:可以从i + 1
开始选 - 选择
i
则有:要从i + questions[i][1] + 1
开始选
可以写一个DFS函数,dfs(i)
表示问题i
到问题n - 1
的最大得分,则有:
- 若i超过了
n
:dfs(i) = 0
- 否则:
dfs(i) = max(dfs(i + 1), dfs(i + questions[i][1] + 1) + questions[i][0])
时空复杂度分析
- 时间复杂度 O ( n ) O(n) O(n),其中 n = l e n ( q u e s t i o n s ) n=len(questions) n=len(questions)
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
/*
* @Author: LetMeFly
* @Date: 2025-04-03 23:18:15
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-04-03 23:24:41
*/
typedef long long ll;
class Solution {
private:
vector<vector<int>> q;
unordered_map<int, ll> cache;
ll dfs(int n) {
if (n >= q.size()) {
return 0;
}
if (cache.count(n)) {
return cache[n];
}
return cache[n] = max(dfs(n + 1), dfs(n + q[n][1] + 1) + q[n][0]);
}
public:
ll mostPoints(vector<vector<int>>& questions) {
q = move(questions);
return dfs(0);
}
};
Python
'''
Author: LetMeFly
Date: 2025-04-03 23:28:40
LastEditors: LetMeFly.xyz
LastEditTime: 2025-04-03 23:31:54
'''
from functools import cache
from typing import List
class Solution:
@cache
def dfs(self, i: int) -> int:
if i >= len(self.q):
return 0
return max(self.dfs(i + 1), self.dfs(i + self.q[i][1] + 1) + self.q[i][0])
def mostPoints(self, questions: List[List[int]]) -> int:
self.q = questions
return self.dfs(0)
Java
/*
* @Author: LetMeFly
* @Date: 2025-04-03 23:32:21
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-04-03 23:38:01
*/
import java.util.Map;
import java.util.HashMap;
class Solution {
private int[][] q;
private Map<Integer, Long> cache = new HashMap<>();
private long dfs(int i) {
if (i >= q.length) {
return 0;
}
if (cache.containsKey(i)) {
return cache.get(i);
}
long ans = Math.max(dfs(i + 1), dfs(i + q[i][1] + 1) + q[i][0]);
cache.put(i, ans);
return ans;
}
public long mostPoints(int[][] questions) {
q = questions;
return dfs(0);
}
}
Go
/*
* @Author: LetMeFly
* @Date: 2025-04-03 23:39:08
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-04-03 23:46:51
*/
package main
var q [][]int
// var cache = make(map[int]int64) // 不可!会被力扣多次调用
var cache map[int]int64 // 这里声明即可,每次在mostPoints时重新赋值
func dfs2140(i int) int64 {
if i >= len(q) {
return 0
}
if val, ok := cache[i]; ok {
return val
}
ans := max(dfs2140(i + 1), dfs2140(i + q[i][1] + 1) + int64(q[i][0]))
cache[i] = ans
return ans
}
func mostPoints(questions [][]int) int64 {
q = questions
cache = make(map[int]int64)
return dfs2140(0)
}
解题方法二:记忆化搜索
同样的:
对于问题i
:
- 不选
i
则有:可以从i + 1
开始选,dp[i] = dp[i + 1]
- 选择
i
则有:要从i + questions[i][1] + 1
开始选,dp[i] = dp[i + questions[i][1] + 1] + questions[i][0]
倒序遍历即可。
时空复杂度分析
- 时间复杂度 O ( n ) O(n) O(n),其中 n = l e n ( q u e s t i o n s ) n=len(questions) n=len(questions)
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
/*
* @Author: LetMeFly
* @Date: 2025-04-03 22:29:08
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-04-03 23:01:43
*/
typedef long long ll;
class Solution {
public:
ll mostPoints(vector<vector<int>>& questions) {
int n = questions.size();
vector<ll> dp(n + 1);
for (int i = n - 1; i >= 0; i--) {
int j = min(i + questions[i][1] + 1, n);
dp[i] = max(dp[i + 1], dp[j] + questions[i][0]);
}
return dp[0];
}
};
Python
'''
Author: LetMeFly
Date: 2025-04-03 23:48:00
LastEditors: LetMeFly.xyz
LastEditTime: 2025-04-03 23:49:25
'''
from typing import List
class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
dp = [0] * (n + 1)
for i in range(n - 1, -1, -1):
j = min(i + questions[i][1] + 1, n)
dp[i] = max(dp[i + 1], dp[j] + questions[i][0])
return dp[0]
Java
/*
* @Author: LetMeFly
* @Date: 2025-04-03 23:50:36
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-04-03 23:52:09
*/
class Solution {
public long mostPoints(int[][] questions) {
int n = questions.length;
long[] dp = new long[n + 1];
for (int i = n - 1; i >= 0; i--) {
int j = Math.min(i + questions[i][1] + 1, n);
dp[i] = Math.max(dp[i + 1], dp[j] + questions[i][0]);
}
return dp[0];
}
}
Go
/*
* @Author: LetMeFly
* @Date: 2025-04-03 23:53:40
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-04-03 23:56:32
*/
package main
func mostPoints(questions [][]int) int64 {
n := len(questions)
dp := make([]int64, n + 1)
for i := n - 1; i >= 0; i-- {
j := min(i + questions[i][1] + 1, n)
dp[i] = max(dp[i + 1], dp[j] + int64(questions[i][0]))
}
return dp[0]
}
同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
千篇源码题解已开源