{"id":9442,"date":"2022-01-30T16:01:07","date_gmt":"2022-01-31T00:01:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9442"},"modified":"2022-01-30T16:05:25","modified_gmt":"2022-01-31T00:05:25","slug":"leetcode-2140-solving-questions-with-brainpower","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2140-solving-questions-with-brainpower\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2140. Solving Questions With Brainpower"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>questions<\/code>&nbsp;where&nbsp;<code>questions[i] = [points<sub>i<\/sub>, brainpower<sub>i<\/sub>]<\/code>.<\/p>\n\n\n\n<p>The array describes the questions of an exam, where you have to process the questions&nbsp;<strong>in order<\/strong>&nbsp;(i.e., starting from question&nbsp;<code>0<\/code>) and make a decision whether to&nbsp;<strong>solve<\/strong>&nbsp;or&nbsp;<strong>skip<\/strong>&nbsp;each question. Solving question&nbsp;<code>i<\/code>&nbsp;will&nbsp;<strong>earn<\/strong>&nbsp;you&nbsp;<code>points<sub>i<\/sub><\/code>&nbsp;points but you will be&nbsp;<strong>unable<\/strong>&nbsp;to solve each of the next&nbsp;<code>brainpower<sub>i<\/sub><\/code>&nbsp;questions. If you skip question&nbsp;<code>i<\/code>, you get to make the decision on the next question.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, given&nbsp;<code>questions = [[3, 2], [4, 3], [4, 4], [2, 5]]<\/code>:<ul><li>If question&nbsp;<code>0<\/code>&nbsp;is solved, you will earn&nbsp;<code>3<\/code>&nbsp;points but you will be unable to solve questions&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>2<\/code>.<\/li><li>If instead, question&nbsp;<code>0<\/code>&nbsp;is skipped and question&nbsp;<code>1<\/code>&nbsp;is solved, you will earn&nbsp;<code>4<\/code>&nbsp;points but you will be unable to solve questions&nbsp;<code>2<\/code>&nbsp;and&nbsp;<code>3<\/code>.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;points you can earn for the exam<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> questions = [[3,2],[4,3],[4,4],[2,5]]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The maximum points can be earned by solving questions 0 and 3.\n- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions\n- Unable to solve questions 1 and 2\n- Solve question 3: Earn 2 points\nTotal points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The maximum points can be earned by solving questions 1 and 4.\n- Skip question 0\n- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions\n- Unable to solve questions 2 and 3\n- Solve question 4: Earn 5 points\nTotal points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= questions.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>questions[i].length == 2<\/code><\/li><li><code>1 &lt;= points<sub>i<\/sub>, brainpower<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>A more general version of <a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-198-house-robber\/\" data-type=\"post\" data-id=\"1112\">\u82b1\u82b1\u9171 LeetCode 198. House Robber<\/a><\/p>\n\n\n\n<p>dp[i] := max points by solving questions[i:n].<br>dp[i] = max(dp[i + b + 1] + points[i] \/* solve *\/ , dp[i+1] \/* skip *\/)<\/p>\n\n\n\n<p>ans = dp[0]<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def mostPoints(self, questions: List[List[int]]) -> int:\n    n = len(questions)\n    @cache\n    def dp(i: int) -> int:\n      if i >= n: return 0\n      p, b = questions[i]      \n      return max(p + dp(i + b + 1), dp(i + 1))\n    \n    return dp(0)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;2D integer array&nbsp;questions&nbsp;where&nbsp;questions[i] = [pointsi, brainpoweri]. The array describes the questions of an exam, where you have to process the questions&nbsp;in order&nbsp;(i.e.,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177],"class_list":["post-9442","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9442"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9442\/revisions"}],"predecessor-version":[{"id":9445,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9442\/revisions\/9445"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}