Solving Questions With Brainpower

You are given a 0-indexed 2D array questions where questions[i] = [points_i, brainpower_i]. You go through the exam in order and for each question decide to solve it or skip it. Solving question i earns points_i but leaves you unable to solve the next brainpower_i questions. Skipping it lets you decide on the next question. Return the maximum points you can earn.

30:00

Solving Questions With Brainpower
medium
Free written solution
Topics
Companies

You are given a 0-indexed 2D array questions where questions[i] = [points_i, brainpower_i]. You go through the exam in order and for each question decide to solve it or skip it. Solving question i earns points_i but leaves you unable to solve the next brainpower_i questions. Skipping it lets you decide on the next question. Return the maximum points you can earn.

Example 1:
Input: {"questions":[[3,2],[4,3],[4,4],[2,5]]}
Output: 5
Constraints:
  • 1≤questions.length≤1051 \leq \text{questions.length} \leq 10^5

  • questions[i].length=2\text{questions}[i].\text{length} = 2

  • 1≤pointsi,brainpoweri≤1051 \leq \text{points}_i, \text{brainpower}_i \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"questions":[[3,2],[4,3],[4,4],[2,5]]}

dp[i] = best score from question i to the end. Past the last question there is nothing left, so dp[4] = 0. Fill dp from right to left.

exam questions 0..4 (points on top, brainpower below)

3bp 204bp 314bp 422bp 53end40dp

→ solve, jump past the blocked ones

⇢ skip to the next question

■ blocked by brainpower

Variables
VariableValue
n4
i-
next-
solve-
skip-
DepthFunction Call
Stack empty
0/11