#2140
Medium Algorithms Solving questions with brainpower
Array Dynamic Programming
60.2% acceptance
Feb 25, 2026
2949
86
You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri].
The array describes the questions of an exam, where you have to process the questions in order and make a decision whether to solve or skip each question. Solving question i will earn you pointsi points but you will be unable to solve each of the next brainpoweri questions.
Return the maximum points you can earn for the exam.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {
let n = questions.len();
let mut dp = vec![0i64; n + 1];
for i in (0..n).rev() {
let next = (i + questions[i][1] as usize + 1).min(n);
dp[i] = dp[i + 1].max(questions[i][0] as i64 + dp[next]);
}
dp[0]
}
}