Leetcode Problem 2140. Solving Questions With Brainpower

2140. Solving Questions With Brainpower

Leetcode Solutions

Dynamic Programming (Iterative)

  1. Initialize an array dp of size n with all elements set to 0.
  2. Set dp[n - 1] to questions[n - 1][0] (points of the last question).
  3. Iterate over the questions in reverse order, starting from n - 2 down to 0.
    • For each question i, calculate the points if we solve it: solve = questions[i][0] + (dp[i + questions[i][1] + 1] if i + questions[i][1] + 1 < n else 0).
    • Calculate the points if we skip it: skip = dp[i + 1].
    • Update dp[i] with the maximum of solve and skip.
  4. Return dp[0] as the result, which represents the maximum points that can be earned from all questions.
UML Thumbnail

Dynamic Programming (Recursive with Memoization)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...