{"id":4313,"date":"2018-11-15T08:37:06","date_gmt":"2018-11-15T16:37:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4313"},"modified":"2018-11-15T08:47:06","modified_gmt":"2018-11-15T16:47:06","slug":"leetcode-55-jump-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-55-jump-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 55. Jump Game"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array of non-negative integers, you are initially positioned at the first index of the array.<\/p>\n<p>Each element in the array represents your maximum jump length at that position.<\/p>\n<p>Determine if you are able to reach the last index.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> [2,3,1,1,4]\r\n<strong>Output:<\/strong> true\r\n<strong>Explanation:<\/strong> Jump 1 step from index 0 to 1, then 3 steps to the last index.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> [3,2,1,0,4]\r\n<strong>Output:<\/strong> false\r\n<strong>Explanation:<\/strong> You will always arrive at index 3 no matter what. Its maximum\r\n\u00a0            jump length is 0, which makes it impossible to reach the last index.\r\n<\/pre>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>If you can jump to i, then you can jump to at least i + nums[i].<\/p>\n<p>Always jump as far as you can.<\/p>\n<p>Keep tracking the farthest index you can jump to.<\/p>\n<p>Init far = nums[0].<\/p>\n<p>far = max(far, i + nums[i])<\/p>\n<p>check far &gt;= n &#8211; 1<\/p>\n<p>ex 1 [2,3,1,1,4]<\/p>\n<pre class=\"lang:sh decode:true\">i    nums[i]   i + nums[i]  far\r\n-      -          -          2\r\n0      2          2          2\r\n1      3          4          4\r\n2      1          3          4\r\n3      1          4          4\r\n4      4          8          8<\/pre>\n<p>ex 2\u00a0[3,2,1,0,4]<\/p>\n<pre class=\"lang:sh decode:true\">i    nums[i]   i + nums[i]  far\r\n-      -          -          3\r\n0      3          3          3\r\n1      2          3          3\r\n2      1          3          3\r\n3      0          3          3 &lt;- can not reach index 4, break<\/pre>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, running time 16 ms\r\nclass Solution {\r\npublic:\r\n  bool canJump(vector&lt;int&gt;&amp; nums) {\r\n    const int n = nums.size();\r\n    int far = nums[0];\r\n    for (int i = 0; i &lt; n; i++) {\r\n      if (i &gt; far) break;\r\n      far = max(far, i + nums[i]);\r\n    }\r\n    return far &gt;= n-1;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem 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&#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":[88,432,177],"class_list":["post-4313","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-jump","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4313","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=4313"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4313\/revisions"}],"predecessor-version":[{"id":4318,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4313\/revisions\/4318"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}