From 468d1e431cac21271c63c3bb5471d268ae2b09c2 Mon Sep 17 00:00:00 2001 From: georgescutelnicu Date: Mon, 7 Apr 2025 19:52:20 +0300 Subject: [PATCH] Create 2140-solving-questions-with-brainpower.py --- .../2140-solving-questions-with-brainpower.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 python/2140-solving-questions-with-brainpower.py 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)