leetcode 2140. Solving Questions With Brainpower

3510 ワード

https://leetcode.com/problems/solving-questions-with-brainpower
class Solution:
    def mostPoints(self, questions: List[List[int]]) -> int:
        def f(qs, idx, memo):
            if idx >= len(qs):return 0
            if idx in memo: return memo[idx]
            p,b = qs[idx]
            skip = f(qs, idx+1,  memo)
            solve = p + f(qs, idx+b+1, memo)
            ans = max(skip, solve)
            memo[idx] = ans
            return ans
        return f(questions, 0, {})