{"id":8487,"date":"2021-08-05T22:26:45","date_gmt":"2021-08-06T05:26:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8487"},"modified":"2021-08-05T22:38:49","modified_gmt":"2021-08-06T05:38:49","slug":"leetcode-1871-jump-game-vii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1871-jump-game-vii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1871. Jump Game VII"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;binary string&nbsp;<code>s<\/code>&nbsp;and two integers&nbsp;<code>minJump<\/code>&nbsp;and&nbsp;<code>maxJump<\/code>. In the beginning, you are standing at index&nbsp;<code>0<\/code>, which is equal to&nbsp;<code>'0'<\/code>. You can move from index&nbsp;<code>i<\/code>&nbsp;to index&nbsp;<code>j<\/code>&nbsp;if the following conditions are fulfilled:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>i + minJump &lt;= j &lt;= min(i + maxJump, s.length - 1)<\/code>, and<\/li><li><code>s[j] == '0'<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if you can reach index&nbsp;<\/em><code>s.length - 1<\/code><em>&nbsp;in&nbsp;<\/em><code>s<\/code><em>, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/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> s = \"011010\", minJump = 2, maxJump = 3\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nIn the first step, move from index 0 to index 3. \nIn the second step, move from index 3 to index 5.\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> s = \"01101110\", minJump = 2, maxJump = 3\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s[i]<\/code>&nbsp;is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><li><code>s[0] == '0'<\/code><\/li><li><code>1 &lt;= minJump &lt;= maxJump &lt; s.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: TreeSet \/Dequq + Binary Search<\/strong><\/h2>\n\n\n\n<p>Maintain a set of reachable indices so far, for each &#8216;0&#8217; index check whether it can be reached from any of the elements in the set.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/set<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 296 ms, 59.7MB\nclass Solution {\npublic:\n  bool canReach(string s, int minJump, int maxJump) {\n    if (s.back() != '0') return false;\n    const int n = s.length();    \n    set<int> seen{0};\n    for (int i = minJump; i < n; ++i) {\n      if (s[i] != '0') continue;\n      while (!seen.empty() &#038;&#038; (*seen.begin()) + maxJump < i)\n        seen.erase(seen.begin());\n      auto it = seen.upper_bound(i - minJump);\n      if (it != seen.begin() &#038;&#038; *prev(it) + minJump <= i)        \n        seen.insert(i);\n    }\n    return seen.count(n - 1);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/deque<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 280 ms, 19MB\nclass Solution {\npublic:\n  bool canReach(string s, int minJump, int maxJump) {\n    if (s.back() != '0') return false;\n    const int n = s.length();    \n    deque<int> seen{0};\n    for (int i = minJump; i < n; ++i) {\n      if (s[i] != '0') continue;\n      while (!seen.empty() &#038;&#038; (seen.front()) + maxJump < i)\n        seen.pop_front();\n      auto it = upper_bound(begin(seen), end(seen), i - minJump);\n      if (it != seen.begin() &#038;&#038; *prev(it) + minJump <= i)\n        seen.push_back(i);\n    }\n    return seen.back() == n - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Queue<\/strong><\/h2>\n\n\n\n<p>Same idea, we can replace the deque in sol1 with a queue, and only check the smallest element in the queue.<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/set<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 56ms, 19.2MB\nclass Solution {\npublic:\n  bool canReach(string s, int minJump, int maxJump) {\n    if (s.back() != '0') return false;\n    const int n = s.length();    \n    queue<int> q{{0}};\n    for (int i = minJump; i < n; ++i) {\n      if (s[i] != '0') continue;\n      while (!q.empty() &#038;&#038; q.front() + maxJump < i)\n        q.pop();\n      if (!q.empty() &#038;&#038; q.front() + minJump <= i)\n        q.push(i);\n    }\n    return q.back() == n - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;binary string&nbsp;s&nbsp;and two integers&nbsp;minJump&nbsp;and&nbsp;maxJump. In the beginning, you are standing at index&nbsp;0, which is equal to&nbsp;&#8216;0&#8217;. You can move from index&nbsp;i&nbsp;to index&nbsp;j&nbsp;if&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177,104,175],"class_list":["post-8487","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","tag-sweep-line","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8487","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=8487"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8487\/revisions"}],"predecessor-version":[{"id":8493,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8487\/revisions\/8493"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}