{"id":4648,"date":"2019-01-15T20:44:12","date_gmt":"2019-01-16T04:44:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4648"},"modified":"2019-01-15T22:10:51","modified_gmt":"2019-01-16T06:10:51","slug":"leetcode-975-odd-even-jump","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-975-odd-even-jump\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 975. Odd Even Jump"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 975. Odd Even Jump - \u5237\u9898\u627e\u5de5\u4f5c EP241\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/MEqDu4hA_Wo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given an integer array&nbsp;<code>A<\/code>.&nbsp; From&nbsp;some starting index, you can make a series of jumps.&nbsp; The (1st, 3rd, 5th, &#8230;)&nbsp;jumps in the series are called&nbsp;<em>odd numbered jumps<\/em>, and the (2nd, 4th, 6th, &#8230;) jumps in the series are called&nbsp;<em>even numbered jumps<\/em>.<\/p>\n\n\n\n<p>You may from index&nbsp;<code>i<\/code>&nbsp;jump forward to index&nbsp;<code>j<\/code>&nbsp;(with&nbsp;<code>i&nbsp;&lt; j<\/code>) in the following way:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>During odd numbered jumps (ie. jumps 1, 3, 5, &#8230;), you jump to the index&nbsp;j&nbsp;such that&nbsp;<code>A[i] &lt;= A[j]<\/code>&nbsp;and&nbsp;<code>A[j]<\/code>&nbsp;is the smallest possible value.&nbsp; If there are multiple such indexes&nbsp;<code>j<\/code>, you can only jump to the&nbsp;<strong>smallest<\/strong>&nbsp;such index&nbsp;<code>j<\/code>.<\/li><li>During even numbered jumps (ie. jumps 2, 4, 6, &#8230;), you jump to the index&nbsp;j&nbsp;such that&nbsp;<code>A[i] &gt;= A[j]<\/code>&nbsp;and&nbsp;<code>A[j]<\/code>&nbsp;is the largest&nbsp;possible value.&nbsp; If there are multiple such indexes&nbsp;<code>j<\/code>, you can only jump to the&nbsp;<strong>smallest<\/strong>&nbsp;such index&nbsp;<code>j<\/code>.<\/li><li>(It may be the case that for some index&nbsp;<code>i,<\/code>&nbsp;there are no legal jumps.)<\/li><\/ul>\n\n\n\n<p>A starting index is&nbsp;<em>good<\/em>&nbsp;if, starting from that index, you can reach the end of the array (index&nbsp;<code>A.length - 1<\/code>) by jumping some number of times (possibly 0 or more than once.)<\/p>\n\n\n\n<p>Return the number of good starting indexes.<\/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>[10,13,12,14,15] <br><strong>Output: <\/strong>2 <\/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>[2,3,1,1,4] <br><strong>Output: <\/strong>3 <\/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>[5,1,3,4,2] <br><strong>Output: <\/strong>3<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 20000<\/code><\/li><li><code>0 &lt;= A[i] &lt; 100000<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Binary Search + DP<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-1.png\" alt=\"\" class=\"wp-image-4652\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-2.png\" alt=\"\" class=\"wp-image-4653\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/01\/975-ep241-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\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++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int oddEvenJumps(vector<int>& A) {\n    const int n = A.size();\n    map<int, int> m;\n    vector<vector<int>> dp(n + 1, vector<int>(2));\n    dp[n - 1][0] = dp[n - 1][1] = 1;\n    m[A[n - 1]] = n - 1;\n    int ans = 1;\n    for (int i = n - 2; i >= 0; --i) {\n      auto o = m.lower_bound(A[i]);\n      if (o != m.end()) dp[i][0] = dp[o->second][1];\n      auto e = m.upper_bound(A[i]);\n      if (e != m.begin()) dp[i][1] = dp[prev(e)->second][0];\n      if (dp[i][0]) ++ans;\n      m[A[i]] = i;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;A.&nbsp; From&nbsp;some starting index, you can make a series of jumps.&nbsp; The (1st, 3rd, 5th, &#8230;)&nbsp;jumps in the series are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[52,18,217,389],"class_list":["post-4648","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-binary-search","tag-dp","tag-hard","tag-monotonic","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4648","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=4648"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4648\/revisions"}],"predecessor-version":[{"id":4655,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4648\/revisions\/4655"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}