{"id":11442,"date":"2022-05-01T03:11:01","date_gmt":"2022-05-01T03:11:01","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11442"},"modified":"2022-05-01T03:11:01","modified_gmt":"2022-05-01T03:11:01","slug":"programming-problem-jump-game-ii","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11442","title":{"rendered":"[Programming Problem] Jump Game II"},"content":{"rendered":"<p>Given an array of non-negative integers nums, 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>Your goal is to reach the last index in the minimum number of jumps.<\/p>\n<p>You can assume that you can always reach the last index.<\/p>\n<p>Example 1:<br \/>\nInput: nums = [2,3,1,1,4]<br \/>\nOutput: 2<br \/>\nExplanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.<\/p>\n<p>Example 2:<br \/>\nInput: nums = [2,3,0,1,4]<br \/>\nOutput: 2<\/p>\n<p>[<a href=\"https:\/\/leetcode.com\/problems\/jump-game-ii\/\" rel=\"noopener\" target=\"_blank\">Programming Problem<\/a>]<\/p>\n<p>At every step jump and update values (if smaller) in the landing step. Let&#8217;s take the example of [2, 3, 1, 1, 4]. We start by initializing the ret array with MAX_VALUE&#8217;s. Then for position 0, we update position 1 and 2 with updated minimum value of 1. We keep doing this for the full array. <\/p>\n<pre lang=\"text\">\r\n[2, 3, 1, 1, 4]\r\n\r\nInitialize           -> [ 0, 1.797e+308, 1.797e+308, 1.797e+308, 1.797e+308 ]\r\nJump from position 0 -> [ 0, 1, 1, 1.797e+308, 1.797e+308 ]\r\nJump from position 1 -> [ 0, 1, 1, 2, 2 ]\r\nJump from position 2 -> [ 0, 1, 1, 2, 2 ]\r\nJump from position 3 -> [ 0, 1, 1, 2, 2 ]\r\nJump from position 4 -> [ 0, 1, 1, 2, 2 ]\r\n<\/pre>\n<pre lang=\"text\">\r\n[3, 0, 0, 1, 4]\r\n\r\nInitialize           -> [ 0, 1.797e+308, 1.797e+308, 1.797e+308, 1.797e+308 ]\r\nJump from position 0 -> [ 0, 1, 1, 1, 1.797e+308 ]\r\nJump from position 1 -> [ 0, 1, 1, 1, 1.797e+308 ]\r\nJump from position 2 -> [ 0, 1, 1, 1, 1.797e+308 ]\r\nJump from position 3 -> [ 0, 1, 1, 1, 2 ]\r\nJump from position 4 -> [ 0, 1, 1, 1, 2 ]\r\n<\/pre>\n<pre lang=\"javascript\">\r\n\/**\r\n * @param {number[]} nums\r\n * @return {number}\r\n *\/\r\nvar jump = function(nums) {\r\n    const ret = [0].concat(new Array(nums.length-1).fill(Number.MAX_VALUE));\r\n    for (let i = 0 ; i < nums.length ; i++ ) {\r\n        let currStep = nums[i];\r\n        \/\/console.log(i, currStep)\r\n        for (let jump = 1 ; jump <= currStep ; jump++) {\r\n            \/\/console.log(\"jump\", jump, i + jump)\r\n            if ( ret[i + jump] > ret[i] + 1 ) {\r\n                ret[i + jump] = ret[i] + 1\r\n            }\r\n        }\r\n    }\r\n    \/\/console.log(ret)\r\n    return ret[nums.length-1]\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of non-negative integers nums, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. You can assume that you can always reach the last [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11442","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11442"}],"version-history":[{"count":2,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11442\/revisions"}],"predecessor-version":[{"id":11444,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11442\/revisions\/11444"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}