From c822deea09f61594d5b52792f76b552970094d59 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:21:52 +0530 Subject: [PATCH] Create 2140. Solving Questions With Brainpower1 --- 2140. Solving Questions With Brainpower1 | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2140. Solving Questions With Brainpower1 diff --git a/2140. Solving Questions With Brainpower1 b/2140. Solving Questions With Brainpower1 new file mode 100644 index 0000000..70fce4e --- /dev/null +++ b/2140. Solving Questions With Brainpower1 @@ -0,0 +1,35 @@ +class Solution { +public: + // Recursive function to calculate the maximum points + long long solve(long long idx, int n, vector>& questions, vector& dp) { + // Base Case: If index exceeds or reaches the last question, return 0 (No more questions left) + if (idx >= n) { + return 0; + } + + // If the answer for this index is already calculated, return it (Memoization step) + if (dp[idx] != -1) { + return dp[idx]; + } + + // **Choice 1: Take the current question** + // - We add the points of the current question + // - Move to the next question index: `idx + questions[idx][1] + 1` (Skipping cooldown questions) + long long take = solve(idx + questions[idx][1] + 1, n, questions, dp) + questions[idx][0]; + + // **Choice 2: Skip the current question** + // - Simply move to the next question (`idx + 1`) + long long not_take = solve(idx + 1, n, questions, dp); + + // Store the maximum result in dp array and return it + return dp[idx] = max(take, not_take); + } + + // Main function to initialize and call recursion + long long mostPoints(vector>& questions) { + int n = questions.size(); // Get total number of questions + vector dp(n + 1, -1); // DP array initialized with -1 (to indicate uncomputed states) + + return solve(0, n, questions, dp); // Start solving from the first question + } +};