Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2025-04-01 23:26:57
2140. Solving Questions With Brainpower
dp问题,还是要想好久
dp[i]表示到第i个question时,不包含question[i][0]所能得到的最大值
每次都去更新dp[i]的值
dp[i]=max(dp[i],dp[i-1])
如果拿了question[i][0]这个值,那就跳到i+question[i][1]+1比较
dp[i]+question[i][0]和dp[i+question[i][1]+1]谁比较大
接着每次更新答案找最大的dp[i]+question[i][0]的组合
golang code :
func mostPoints(questions [][]int) int64 {
n, ans := len(questions), 0
dp := make([]int, n+1)
for i := 0; i < n; i++ {
nextIdx := i + questions[i][1] + 1
dp[i+1] = max(dp[i+1], dp[i])
if nextIdx < n {
dp[nextIdx+1] = max(dp[nextIdx+1], dp[i+1]+questions[i][0])
}
ans = max(ans, questions[i][0]+dp[i+1])
}
return int64(ans)
}

Links booklink

Contact Us: admin [ a t ] ucptt.com