Solving Questions with Brainpower
Last updated
Last updated
For question i
, either:
Solve the question, gain the points, and only be able to answer the i + cooldown
problem onwards
Skip the question
The recurrence relation, much like other Linear Sequence problems is obtained by directly mapping the transitions.
The recurrence is the same as the top-down approach as we need to have information about the "next" state, so prefixes will not be an ideal solution for this.
This means we will have to iterate from the back. We also add a padding of 0
towards the end of the array to represent the case when i + 1
or i + questions[i][1] + 1
exceed N
(length of questions).
Trick: Boundary conditions
Very often you will encounter state transitions that can cause out of bounds such as i + 1 >= N
above. In these scenarios, you can introduction boundary conditions by modifying the dp
array to include additional leading/trailing elements.
If the state transition may go very out of bounds like i + questions[i][1] + 1
, then you can use min(transition, N)
to default it to use the boundary condition.
The value of the boundary condition is often the case when there is nothing to process. In this case, we add 0
as that's the default value that exists when there's "no questions".
Actually, there is a way to implement the bottom-up solution using the "re-framing the problem" trick.
Given that we want to solve question
i
, what's the maximum points achievable?
By thinking of the problem this way, we can devise an approach to figure out how to forcibly "solve" question i
. We can iterate through 0..i-1
to find all days where we would have solved it and the cooldown does not exceed i
. This results in the following recurrence instead:
However, I would like to bring this up as an alternative thought process to highlight how sometimes "re-framing" the problem might not always be the optimal solution.
The reason we do not take is because if is chosen, we might not be able to solve question i
. The only problem with this solution is that it takes time to compute which does not fit within the constraints of the problem.