|
| 1 | + |
| 2 | + |
| 3 | +# 2140. Solving Questions With Brainpower |
| 4 | + |
| 5 | +## 📌 Problem Statement |
| 6 | +You are given a 0-indexed 2D integer array `questions` where `questions[i] = [pointsi, brainpoweri]`. |
| 7 | +For each question, you must decide whether to **solve** it or **skip** it: |
| 8 | +- **Solve:** Earn `pointsi` points, but you must skip the next `brainpoweri` questions. |
| 9 | +- **Skip:** Move to the next question without earning any points. |
| 10 | + |
| 11 | +The goal is to maximize the total points earned by processing the questions in order. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 📊 Table Structure |
| 16 | + |
| 17 | +Although this problem is algorithmic in nature and doesn't use a database, we can imagine a **Questions Table** that represents each question. For demonstration purposes, we’ll show a sample table structure similar to a "Teacher Table": |
| 18 | + |
| 19 | +### Teacher Table (Questions Table) |
| 20 | +| QuestionID | Points | Brainpower | |
| 21 | +| ---------- | ------ | ---------- | |
| 22 | +| 0 | 3 | 2 | |
| 23 | +| 1 | 4 | 3 | |
| 24 | +| 2 | 4 | 4 | |
| 25 | +| 3 | 2 | 5 | |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 📊 Example 1 |
| 30 | + |
| 31 | +### Input: Teacher Table |
| 32 | +| QuestionID | Points | Brainpower | |
| 33 | +| ---------- | ------ | ---------- | |
| 34 | +| 0 | 3 | 2 | |
| 35 | +| 1 | 4 | 3 | |
| 36 | +| 2 | 4 | 4 | |
| 37 | +| 3 | 2 | 5 | |
| 38 | + |
| 39 | +### Output: |
| 40 | +``` |
| 41 | +5 |
| 42 | +``` |
| 43 | + |
| 44 | +### Explanation: |
| 45 | +- **Solve question 0:** Earn 3 points, then skip questions 1 and 2. |
| 46 | +- **Solve question 3:** Earn 2 points. |
| 47 | +- **Total points:** 3 + 2 = 5. |
| 48 | + |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## ✅ Approach: Python (Recursion with Memoization) |
| 53 | +We use a recursive depth-first search (DFS) with memoization to decide for each question whether to solve it or skip it. |
| 54 | +For each question `i`: |
| 55 | +- **Solve:** Add `pointsi` and then jump to question `i + brainpoweri + 1`. |
| 56 | +- **Skip:** Move to question `i + 1`. |
| 57 | + |
| 58 | +The recurrence is: |
| 59 | +```python |
| 60 | +dfs(i) = max(pointsi + dfs(i + brainpoweri + 1), dfs(i + 1)) |
| 61 | +``` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## 🐍 Python (Pandas) Solution 1 |
| 66 | + |
| 67 | +```python |
| 68 | +from functools import cache |
| 69 | +from typing import List |
| 70 | + |
| 71 | +class Solution: |
| 72 | + def mostPoints(self, questions: List[List[int]]) -> int: |
| 73 | + @cache |
| 74 | + def dfs(i: int) -> int: |
| 75 | + if i >= len(questions): |
| 76 | + return 0 |
| 77 | + p, b = questions[i] |
| 78 | + return max(p + dfs(i + b + 1), dfs(i + 1)) |
| 79 | + |
| 80 | + return dfs(0) |
| 81 | +``` |
| 82 | + |
| 83 | +## 🐍 Python (Pandas) Solution 2 |
| 84 | + |
| 85 | +```python |
| 86 | +class Solution: |
| 87 | + def mostPoints(self, questions: List[List[int]]) -> int: |
| 88 | + @cache |
| 89 | + def dfs(i: int) -> int: |
| 90 | + if i >= len(questions): |
| 91 | + return 0 |
| 92 | + p, b = questions[i] |
| 93 | + return max(p + dfs(i + b + 1), dfs(i + 1)) |
| 94 | + |
| 95 | + return dfs(0) |
| 96 | +``` |
| 97 | + |
| 98 | +### ✅ Approach Explanation: |
| 99 | +- **Recursion with Memoization:** The function `dfs(i)` computes the maximum points achievable starting from the `i`th question. |
| 100 | +- **Decision:** For each question, it chooses the maximum between solving the question (adding its points and skipping the next `brainpower` questions) and skipping the question. |
| 101 | +- **Efficiency:** The use of caching (`@cache`) ensures each state is computed only once. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## 📁 File Structure |
| 106 | + |
| 107 | +```plaintext |
| 108 | +. |
| 109 | +├── README.md # This file |
| 110 | +└── solution.py # Contains the Python solution implementation |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## 🔗 Useful Links |
| 116 | +- [LeetCode Problem 2140](https://leetcode.com/problems/solving-questions-with-brainpower/) |
| 117 | +- [Dynamic Programming - Wikipedia](https://en.wikipedia.org/wiki/Dynamic_programming) |
| 118 | +- [Python functools.cache Documentation](https://docs.python.org/3/library/functools.html#functools.cache) |
0 commit comments