{"id":5118,"date":"2019-04-28T11:44:27","date_gmt":"2019-04-28T18:44:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5118"},"modified":"2019-04-28T11:51:22","modified_gmt":"2019-04-28T18:51:22","slug":"leetcode-45-jump-game-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-45-jump-game-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 45. Jump Game II"},"content":{"rendered":"\n<p>Given an array of non-negative integers, you are initially positioned at the first index of the array.<\/p>\n\n\n\n<p>Each element in the array represents your maximum jump length at that position.<\/p>\n\n\n\n<p>Your goal is to reach the last index in the minimum number of jumps.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [2,3,1,1,4]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The minimum number of jumps to reach the last index is 2.\n    Jump 1 step from index 0 to 1, then 3 steps to the last index.<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<p>You can assume that you can always reach the last index.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Jump as far as possible but lazily. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">\n[2, 3, 1, 1, 4]\ni    nums[i]   steps   near   far\n-      -         0       0     0\n0      2         0       0     2\n1      3         1       2     4\n2      1         1       2     4\n3      1         2       4     4\n4      4         2       4     8\n<\/pre>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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, running time: 12 ms \/ 10.3 MB\nclass Solution {\npublic:\n  int jump(vector<int>& nums) {\n    int steps = 0;\n    int near = 0;\n    int far = 0;\n    for (int i = 0; i < nums.size(); ++i) {\n      if (i > near) {\n        ++steps;\n        near = far;\n      }\n      far = max(far, i + nums[i]);      \n    }\n    return steps;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[27,88,432],"class_list":["post-5118","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-game","tag-greedy","tag-jump","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5118","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=5118"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5118\/revisions"}],"predecessor-version":[{"id":5123,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5118\/revisions\/5123"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}