{"id":6025,"date":"2019-12-29T11:18:46","date_gmt":"2019-12-29T19:18:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6025"},"modified":"2019-12-29T11:21:52","modified_gmt":"2019-12-29T19:21:52","slug":"leetcode-1306-jump-game-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1306-jump-game-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1306. Jump Game III"},"content":{"rendered":"\n<p>Given an array of non-negative integers&nbsp;<code>arr<\/code>, you are initially positioned at&nbsp;<code>start<\/code>&nbsp;index of the array. When you are at index&nbsp;<code>i<\/code>, you can jump&nbsp;to&nbsp;<code>i + arr[i]<\/code>&nbsp;or&nbsp;<code>i - arr[i]<\/code>, check if you can reach to&nbsp;<strong>any<\/strong>&nbsp;index with value 0.<\/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 = [4,2,3,0,3,1,2], start = 5\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> \nAll possible ways to reach at index 3 with value 0 are: \nindex 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 \nindex 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 \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 = [4,2,3,0,3,1,2], start = 0\n<strong>Output:<\/strong> true \n<strong>Explanation: \n<\/strong>One possible way to reach at index 3 with value 0 is: \nindex 0 -&gt; index 4 -&gt; index 1 -&gt; index 3\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 = [3,0,2,1,2], start = 2\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>There is no way to reach at index 1 with value 0.\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>0 &lt;= arr[i] &lt;&nbsp;arr.length<\/code><\/li><li><code>0 &lt;= start &lt; arr.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS \/ DFS<\/strong><\/h2>\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\nclass Solution {\npublic:\n  bool canReach(vector<int>& arr, int start) {\n    const int n = arr.size();\n    vector<int> seen(n);\n    seen[start] = 1;\n    queue<int> q;\n    q.push(start);\n    while (q.size()) {\n      int cur = q.front();\n      q.pop();\n      if (arr[cur] == 0) return true;\n      for (int i : {-1, 1}) {\n        int t = cur + i * arr[cur];\n        if (t < 0 || t >= n || seen[t]) continue;\n        q.push(t);\n        seen[t] = 1;        \n      }\n    }\n    return false;\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&nbsp;arr, you are initially positioned at&nbsp;start&nbsp;index of the array. When you are at index&nbsp;i, you can jump&nbsp;to&nbsp;i + arr[i]&nbsp;or&nbsp;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,33,177,376,42],"class_list":["post-6025","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-dfs","tag-medium","tag-on","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6025","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=6025"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6025\/revisions"}],"predecessor-version":[{"id":6028,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6025\/revisions\/6028"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}