logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
2. Add Two Numbers3. Longest Substring Without Repeating Characters5. Longest Palindromic Substring8. String to Integer (atoi)11. Container With Most Water15. 3Sum17. Letter Combinations of a Phone Number19. Remove Nth Node From End of List22. Generate Parentheses33. Search in Rotated Sorted Array34. Find First and Last Position of Element in Sorted Array48. Rotate Image49. Group Anagrams50. Pow(x, n)53. Maximum Subarray54. Spiral Matrix56. Merge Intervals61. Rotate List73. Set Matrix Zeroes74. Search a 2D Matrix75. Sort Colors78. Subsets137. Single Number II138. Copy List with Random Pointer150. Evaluate Reverse Polish Notation151. Reverse Words in a String153. Find Minimum in Rotated Sorted Array155. Min Stack162. Find Peak Element169. Majority Element198. House Robber207. Course Schedule215. Kth Largest Element in an Array227. Basic Calculator II236. Lowest Common Ancestor of a Binary Tree237. Delete Node in a Linked List238. Product of Array Except Self451. Sort Characters By Frequency540. Single Element in a Sorted Array560. Subarray Sum Equals K636. Exclusive Time of Functions875. Koko Eating Bananas1011. Capacity To Ship Packages Within D Days1101. The Earliest Moment When Everyone Become Friends1283. Find the Smallest Divisor Given a Threshold2140. Solving Questions With Brainpower3169. Count Days Without Meetings3481. Apply Substitutions
Code Scratch/leetcode/medium

2140. Solving Questions With Brainpower

2 min read
On this pageIntuition
IntuitionComplexityCode

# Intuition

This is a classic Dynamic Programming problem where at each question, we decide between:

  • Solving it (and skipping the next brainpower[i] questions),
  • Or skipping it and checking the next one.

We use memoization to avoid recalculating the maximum points for the same index again.

# Complexity

Space ComplexityTime Complexity
O(N)\text{O}(N)O(N)O(N)\text{O}(N)O(N)

# Code

// Memoization array to store max points from each index
long[] memo;
 
// Recursive DP function to compute maximum points from question `index`
long pointsCalc(int[][] questions, int index) {
    // Base case: if index is out of bounds, no points can be earned
    if (index >= questions.length) {
        return 0;
    }
 
    // If result already computed, return it
    if (memo[index] != -1) return memo[index];
 
    // Option 1: Skip the current question and go to the next one
    long skipPoints = pointsCalc(questions, index + 1);
 
    // Option 2: Solve the current question
    // Add its points and skip the next `brainpower` questions
    long solvePoints = questions[index][0] + pointsCalc(questions, index + 1 + questions[index][1]);
 
    // Store the maximum of both choices in memo and return it
    memo[index] = Math.max(skipPoints, solvePoints);
    return memo[index];
}
 
public long mostPoints(int[][] questions) {
    // Initialize memoization array with -1
    memo = new long[questions.length + 1];
    Arrays.fill(memo, -1);
 
    // Start solving from the 0th question
    return pointsCalc(questions, 0);
}
 
←Previous1283. Find the Smallest Divisor Given a ThresholdNext→3169. Count Days Without Meetings

On this page

IntuitionComplexityCode