Skip to content

Commit 7e67f68

Browse files
committed
day1: Always Day 1
update: 添加问题“2140.解决智力问题”的代码和题解(#855) MGJW: 周报.(本周->上周) Signed-off-by: LetMeFly666 <[email protected]>
1 parent 151f949 commit 7e67f68

13 files changed

+541
-10
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-03 23:18:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-03 23:24:41
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
13+
class Solution {
14+
private:
15+
vector<vector<int>> q;
16+
unordered_map<int, ll> cache;
17+
18+
ll dfs(int n) {
19+
if (n >= q.size()) {
20+
return 0;
21+
}
22+
if (cache.count(n)) {
23+
return cache[n];
24+
}
25+
return cache[n] = max(dfs(n + 1), dfs(n + q[n][1] + 1) + q[n][0]);
26+
}
27+
public:
28+
ll mostPoints(vector<vector<int>>& questions) {
29+
q = move(questions);
30+
return dfs(0);
31+
}
32+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-03 23:39:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-03 23:46:51
6+
*/
7+
package main
8+
9+
var q [][]int
10+
// var cache = make(map[int]int64) // 不可!会被力扣多次调用
11+
var cache map[int]int64 // 这里声明即可,每次在mostPoints时重新赋值
12+
13+
func dfs2140(i int) int64 {
14+
if i >= len(q) {
15+
return 0
16+
}
17+
if val, ok := cache[i]; ok {
18+
return val
19+
}
20+
ans := max(dfs2140(i + 1), dfs2140(i + q[i][1] + 1) + int64(q[i][0]))
21+
cache[i] = ans
22+
return ans
23+
}
24+
25+
func mostPoints(questions [][]int) int64 {
26+
q = questions
27+
cache = make(map[int]int64)
28+
return dfs2140(0)
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-03 23:32:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-03 23:38:01
6+
*/
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
10+
class Solution {
11+
private int[][] q;
12+
private Map<Integer, Long> cache = new HashMap<>();
13+
14+
private long dfs(int i) {
15+
if (i >= q.length) {
16+
return 0;
17+
}
18+
if (cache.containsKey(i)) {
19+
return cache.get(i);
20+
}
21+
long ans = Math.max(dfs(i + 1), dfs(i + q[i][1] + 1) + q[i][0]);
22+
cache.put(i, ans);
23+
return ans;
24+
}
25+
26+
public long mostPoints(int[][] questions) {
27+
q = questions;
28+
return dfs(0);
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-03 23:28:40
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-03 23:31:54
6+
'''
7+
from functools import cache
8+
from typing import List
9+
10+
11+
class Solution:
12+
@cache
13+
def dfs(self, i: int) -> int:
14+
if i >= len(self.q):
15+
return 0
16+
return max(self.dfs(i + 1), self.dfs(i + self.q[i][1] + 1) + self.q[i][0])
17+
18+
def mostPoints(self, questions: List[List[int]]) -> int:
19+
self.q = questions
20+
return self.dfs(0)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-03 23:53:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-03 23:56:32
6+
*/
7+
package main
8+
9+
func mostPoints(questions [][]int) int64 {
10+
n := len(questions)
11+
dp := make([]int64, n + 1)
12+
for i := n - 1; i >= 0; i-- {
13+
j := min(i + questions[i][1] + 1, n)
14+
dp[i] = max(dp[i + 1], dp[j] + int64(questions[i][0]))
15+
}
16+
return dp[0]
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-03 23:50:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-03 23:52:09
6+
*/
7+
class Solution {
8+
public long mostPoints(int[][] questions) {
9+
int n = questions.length;
10+
long[] dp = new long[n + 1];
11+
for (int i = n - 1; i >= 0; i--) {
12+
int j = Math.min(i + questions[i][1] + 1, n);
13+
dp[i] = Math.max(dp[i + 1], dp[j] + questions[i][0]);
14+
}
15+
return dp[0];
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-03 23:48:00
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-03 23:49:25
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def mostPoints(self, questions: List[List[int]]) -> int:
11+
n = len(questions)
12+
dp = [0] * (n + 1)
13+
for i in range(n - 1, -1, -1):
14+
j = min(i + questions[i][1] + 1, n)
15+
dp[i] = max(dp[i + 1], dp[j] + questions[i][0])
16+
return dp[0]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-03 22:29:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-03 23:01:43
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
12+
#ifdef FirstTry // WA // 不可:将当前经验值直接累加到后面
13+
/*
14+
[[1,1],[2,2],[3,3],[4,4],[5,5]]
15+
cnt 0 0 1 1 1+2
16+
dp 1 2 4 5 8
17+
18+
real ans: 7
19+
*/
20+
typedef long long ll;
21+
22+
class Solution {
23+
public:
24+
ll mostPoints(vector<vector<int>>& questions) {
25+
vector<ll> dp(questions.size());
26+
ll cnt = 0;
27+
for (int i = 0; i < questions.size(); i++) {
28+
cnt += dp[i];
29+
dp[i] = cnt + questions[i][0];
30+
if (i + questions[i][1] + 1 < questions.size()) {
31+
dp[i + questions[i][1] + 1] += questions[i][0];
32+
}
33+
}
34+
return dp.back();
35+
}
36+
};
37+
#else // FirstTry
38+
// SecondTry
39+
typedef long long ll;
40+
41+
class Solution {
42+
public:
43+
ll mostPoints(vector<vector<int>>& questions) {
44+
int n = questions.size();
45+
vector<ll> dp(n + 1);
46+
for (int i = n - 1; i >= 0; i--) {
47+
int j = min(i + questions[i][1] + 1, n);
48+
dp[i] = max(dp[i + 1], dp[j] + questions[i][0]);
49+
}
50+
return dp[0];
51+
}
52+
};
53+
#endif // FirstTry

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@
664664
|2116.判断一个括号字符串是否有效|中等|<a href="https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/23/LeetCode%202116.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%8B%AC%E5%8F%B7%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146454056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/solutions/3624225/letmefly-2116pan-duan-yi-ge-gua-hao-zi-f-9n2z/" target="_blank">LeetCode题解</a>|
665665
|2129.将标题首字母大写|简单|<a href="https://leetcode.cn/problems/capitalize-the-title/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/11/LeetCode%202129.%E5%B0%86%E6%A0%87%E9%A2%98%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136614914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/capitalize-the-title/solutions/2679708/letmefly-2129jiang-biao-ti-shou-zi-mu-da-nj90/" target="_blank">LeetCode题解</a>|
666666
|2132.用邮票贴满网格图|困难|<a href="https://leetcode.cn/problems/stamping-the-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/14/LeetCode%202132.%E7%94%A8%E9%82%AE%E7%A5%A8%E8%B4%B4%E6%BB%A1%E7%BD%91%E6%A0%BC%E5%9B%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135002925" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/2566644/letmefly-2132yong-you-piao-tie-man-wang-znigh/" target="_blank">LeetCode题解</a>|
667+
|2140.解决智力问题|中等|<a href="https://leetcode.cn/problems/solving-questions-with-brainpower/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/03/LeetCode%202140.%E8%A7%A3%E5%86%B3%E6%99%BA%E5%8A%9B%E9%97%AE%E9%A2%98/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146991317" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/solving-questions-with-brainpower/solutions/3639601/letmefly-2140jie-jue-zhi-li-wen-ti-ji-yi-pxc4/" target="_blank">LeetCode题解</a>|
667668
|2171.拿出最少数目的魔法豆|中等|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/18/LeetCode%202171.%E6%8B%BF%E5%87%BA%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E9%AD%94%E6%B3%95%E8%B1%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135682601" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/solutions/2609831/letmefly-2171na-chu-zui-shao-shu-mu-de-m-jima/" target="_blank">LeetCode题解</a>|
668669
|2178.拆分成最多数目的正偶数之和|中等|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/06/LeetCode%202178.%E6%8B%86%E5%88%86%E6%88%90%E6%9C%80%E5%A4%9A%E6%95%B0%E7%9B%AE%E7%9A%84%E6%AD%A3%E5%81%B6%E6%95%B0%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131567568" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/solutions/2332513/letmefly-2178chai-fen-cheng-zui-duo-shu-m44er/" target="_blank">LeetCode题解</a>|
669670
|2180.统计各位数字之和为偶数的整数个数|简单|<a href="https://leetcode.cn/problems/count-integers-with-even-digit-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/06/LeetCode%202180.%E7%BB%9F%E8%AE%A1%E5%90%84%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B9%8B%E5%92%8C%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B4%E6%95%B0%E4%B8%AA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128583772" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/2048274/letmefly-2180tong-ji-ge-wei-shu-zi-zhi-h-g2ib/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)