diff --git a/python/2140-solving-questions-with-brainpower.py b/python/2140-solving-questions-with-brainpower.py new file mode 100644 index 000000000..8306264e7 --- /dev/null +++ b/python/2140-solving-questions-with-brainpower.py @@ -0,0 +1,19 @@ +class Solution: + def mostPoints(self, questions: List[List[int]]) -> int: + + cache = [0] * len(questions) + + def backtrack(idx): + if idx >= len(questions): + return 0 + if cache[idx]: + return cache[idx] + + points, brainpower = questions[idx] + + cache[idx] = max(backtrack(idx + 1), + points + backtrack(idx + 1 + brainpower)) + + return cache[idx] + + return backtrack(0)