{"id":6269,"date":"2020-02-08T09:29:47","date_gmt":"2020-02-08T17:29:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6269"},"modified":"2020-02-08T09:34:00","modified_gmt":"2020-02-08T17:34:00","slug":"leetcode-1345-jump-game-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1345-jump-game-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1345. Jump Game IV"},"content":{"rendered":"\n<p>Given an array of&nbsp;integers&nbsp;<code>arr<\/code>, you are initially positioned at the first index of the array.<\/p>\n\n\n\n<p>In one step you can jump from index&nbsp;<code>i<\/code>&nbsp;to index:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>i + 1<\/code>&nbsp;where:&nbsp;<code>i + 1 &lt; arr.length<\/code>.<\/li><li><code>i - 1<\/code>&nbsp;where:&nbsp;<code>i - 1 &gt;= 0<\/code>.<\/li><li><code>j<\/code>&nbsp;where:&nbsp;<code>arr[i] == arr[j]<\/code>&nbsp;and&nbsp;<code>i != j<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the minimum number of steps<\/em>&nbsp;to reach the&nbsp;<strong>last index<\/strong>&nbsp;of the array.<\/p>\n\n\n\n<p>Notice that you can not jump outside of the array at any time.<\/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> arr = [100,-23,-23,404,100,23,23,23,3,404]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> You need three jumps from index 0 --&gt; 4 --&gt; 3 --&gt; 9. Note that index 9 is the last index of the array.\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> arr = [7]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Start index is the last index. You don't need to jump.\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> arr = [7,6,9,6,9,6,9,7]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> You can jump directly from index 0 to index 7 which is last index of the array.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [6,1,9]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [11,22,7,7,7,7,7,7,7,22,13]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 5 * 10^4<\/code><\/li><li><code>-10^8 &lt;= arr[i] &lt;= 10^8<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable + BFS<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to store the indices of each unique number.<\/p>\n\n\n\n<p>each index i has neighbors (i-1, i + 1, hashtable[arr[i]])<\/p>\n\n\n\n<p>Use BFS to find the shortest path in this unweighted graph.<\/p>\n\n\n\n<p>Key optimization, clear hashtable[arr[i]] after the first use, since all nodes are already on queue, no longer needed.<\/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\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 124 ms, 30.5 MB\nclass Solution {\npublic:\n  int minJumps(vector<int>& arr) {\n    const int n = arr.size();\n    unordered_map<int, vector<int>> m;\n    for (int i = 0; i < n; ++i)\n      m[arr[i]].push_back(i);\n    vector<int> seen(n);\n    queue<int> q({0});    \n    seen[0] = 1;\n    int steps = 0;\n    while (!q.empty()) {\n      int size = q.size();\n      while (size--) {\n        int i = q.front(); q.pop();        \n        if (i == n - 1) return steps;\n        if (i - 1 >= 0 && !seen[i - 1]++) q.push(i - 1);\n        if (i + 1 < n &#038;&#038; !seen[i + 1]++) q.push(i + 1);\n        auto it = m.find(arr[i]);\n        if (it == m.end()) continue;\n        for (int nxt : it->second)\n          if (!seen[nxt]++) q.push(nxt);\n        m.erase(it); \/\/ no longer needed.\n      }\n      ++steps;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-55-jump-game\/\">https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-55-jump-game\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-45-jump-game-ii\/\">https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-45-jump-game-ii\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1306-jump-game-iii\/\">https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1306-jump-game-iii\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1344-jump-game-v\/\">https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1344-jump-game-v\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of&nbsp;integers&nbsp;arr, you are initially positioned at the first index of the array. In one step you can jump from index&nbsp;i&nbsp;to index: i&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[34,217,82,548],"class_list":["post-6269","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-hard","tag-hashtable","tag-jump-game","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6269","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=6269"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6269\/revisions"}],"predecessor-version":[{"id":6273,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6269\/revisions\/6273"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}