{"id":7827,"date":"2020-12-20T01:19:34","date_gmt":"2020-12-20T09:19:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7827"},"modified":"2020-12-20T10:07:00","modified_gmt":"2020-12-20T18:07:00","slug":"leetcode-1696-jump-game-vi","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1696-jump-game-vi\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1696. Jump Game VI"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1696. Jump Game VI - \u5237\u9898\u627e\u5de5\u4f5c EP376\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/M_PzYd59_kk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>You are initially standing at index&nbsp;<code>0<\/code>. In one move, you can jump at most&nbsp;<code>k<\/code>&nbsp;steps forward without going outside the boundaries of the array. That is, you can jump from index&nbsp;<code>i<\/code>&nbsp;to any index in the range&nbsp;<code>[i + 1, min(n - 1, i + k)]<\/code>&nbsp;<strong>inclusive<\/strong>.<\/p>\n\n\n\n<p>You want to reach the last index of the array (index&nbsp;<code>n - 1<\/code>). Your&nbsp;<strong>score<\/strong>&nbsp;is the&nbsp;<strong>sum<\/strong>&nbsp;of all&nbsp;<code>nums[j]<\/code>&nbsp;for each index&nbsp;<code>j<\/code>&nbsp;you visited in the array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum score<\/strong>&nbsp;you can get<\/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> nums = [1,-1,-2,4,-7,3], k = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> You can choose your jumps forming the subsequence [1,-1,4,3] (underlined above). The sum is 7.\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> nums = [10,-5,-2,4,0,3], k = 3\n<strong>Output:<\/strong> 17\n<strong>Explanation:<\/strong> You can choose your jumps forming the subsequence [10,4,3] (underlined above). The sum is 17.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,-5,-20,4,-1,3,-6,-3], k = 2\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&nbsp;<code>1 &lt;= nums.length, k &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>4<\/sup>&nbsp;&lt;= nums[i]&nbsp;&lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP + Monotonic Queue<\/strong><\/h2>\n\n\n\n<p>dp[i] = nums[i] + max(dp[j]) i &#8211; k &lt;= j &lt; i<\/p>\n\n\n\n<p>Brute force time complexity: O(n*k) =&gt; TLE<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-1.png\" alt=\"\" class=\"wp-image-7833\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-2.png\" alt=\"\" class=\"wp-image-7834\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-3.png\" alt=\"\" class=\"wp-image-7835\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1696-ep376-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3 \/ TLE<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\"># Author: Huahua 52\/58 Passed (TLE)\nclass Solution:  \n  def maxResult(self, nums: List[int], k: int) -&gt; int:\n    @lru_cache(None)\n    def dp(i: int) -&gt; int:\n      return nums[0] if i == 0 else nums[i] + max(dp(j) for j in range(max(0, i - k), i))\n    return dp(len(nums) - 1)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>This problem can be reduced to find the maximum for a sliding window that can be solved by monotonic queue.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n+k) -> O(k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxResult(vector<int>& nums, int k) {\n    const int n = nums.size();\n    vector<int> dp(n);\n    deque<int> q{{0}};\n    dp[0] = nums[0];    \n    for (int i = 1; i < n; ++i) {\n      dp[i] = nums[i] + dp[q.front()];\n      while (!q.empty() &#038;&#038; dp[i] >= dp[q.back()]) q.pop_back();\n      while (!q.empty() && i - q.front() >= k) q.pop_front();      \n      q.push_back(i);      \n    }\n    return dp[n - 1];\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/O(n) Space<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxResult(vector<int>& nums, int k) {\n    const int n = nums.size();\n    deque<pair<int, int>> q{{nums[0], 0}};\n    for (int i = 1; i < n; ++i) {      \n      const int cur = nums[i] + q.front().first;      \n      while (!q.empty() &#038;&#038; cur >= q.back().first) \n        q.pop_back();\n      while (!q.empty() && i - q.front().second >= k) \n        q.pop_front();      \n      q.emplace_back(cur, i);\n    }\n    for (const auto& [v, i] : q)\n      if (i == n - 1) return v;\n    return 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;and an integer&nbsp;k. You are initially standing at index&nbsp;0. In one move, you can jump at most&nbsp;k&nbsp;steps forward without going outside&#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,587],"class_list":["post-7827","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-monotonic-queue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7827","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=7827"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7827\/revisions"}],"predecessor-version":[{"id":7837,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7827\/revisions\/7837"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}