|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +## 975. Odd Even Jump (Hard) |
| 9 | + |
| 10 | +<p>You are given an integer array <code>A</code>. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called <em>odd numbered jumps</em>, and the (2nd, 4th, 6th, ...) jumps in the series are called <em>even numbered jumps</em>.</p> |
| 11 | + |
| 12 | +<p>You may from index <code>i</code> jump forward to index <code><font face="monospace">j</font></code> (with <code>i < j</code>) in the following way:</p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li>During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index <font face="monospace">j</font> such that <code>A[i] <= A[j]</code> and <code>A[j]</code> is the smallest possible value. If there are multiple such indexes <code><font face="monospace">j</font></code>, you can only jump to the <strong>smallest</strong> such index <code><font face="monospace">j</font></code>.</li> |
| 16 | + <li>During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index <font face="monospace">j</font> such that <code>A[i] >= A[j]</code> and <code>A[j]</code> is the largest possible value. If there are multiple such indexes <code><font face="monospace">j</font></code>, you can only jump to the <strong>smallest</strong> such index <code><font face="monospace">j</font></code>.</li> |
| 17 | + <li>(It may be the case that for some index <code><font face="monospace">i</font>,</code> there are no legal jumps.)</li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p>A starting index is <em>good</em> if, starting from that index, you can reach the end of the array (index <code>A.length - 1</code>) by jumping some number of times (possibly 0 or more than once.)</p> |
| 21 | + |
| 22 | +<p>Return the number of good starting indexes.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p><strong>Example 1:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input: </strong><span id="example-input-1-1">[10,13,12,14,15]</span> |
| 30 | +<strong>Output: </strong><span id="example-output-1">2</span> |
| 31 | +<strong>Explanation: </strong> |
| 32 | +From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. |
| 33 | +From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. |
| 34 | +From starting index i = 3, we can jump to i = 4, so we've reached the end. |
| 35 | +From starting index i = 4, we've reached the end already. |
| 36 | +In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<div> |
| 40 | +<p><strong>Example 2:</strong></p> |
| 41 | + |
| 42 | +<pre> |
| 43 | +<strong>Input: </strong><span id="example-input-2-1">[2,3,1,1,4]</span> |
| 44 | +<strong>Output: </strong><span id="example-output-2">3</span> |
| 45 | +<strong>Explanation: </strong> |
| 46 | +From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: |
| 47 | + |
| 48 | +During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. |
| 49 | + |
| 50 | +During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. |
| 51 | + |
| 52 | +During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. |
| 53 | + |
| 54 | +We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. |
| 55 | + |
| 56 | +In a similar manner, we can deduce that: |
| 57 | +From starting index i = 1, we jump to i = 4, so we reach the end. |
| 58 | +From starting index i = 2, we jump to i = 3, and then we can't jump anymore. |
| 59 | +From starting index i = 3, we jump to i = 4, so we reach the end. |
| 60 | +From starting index i = 4, we are already at the end. |
| 61 | +In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. |
| 62 | +</pre> |
| 63 | + |
| 64 | +<div> |
| 65 | +<p><strong>Example 3:</strong></p> |
| 66 | + |
| 67 | +<pre> |
| 68 | +<strong>Input: </strong><span id="example-input-3-1">[5,1,3,4,2]</span> |
| 69 | +<strong>Output: </strong><span id="example-output-3">3</span> |
| 70 | +<strong>Explanation: </strong> |
| 71 | +We can reach the end from starting indexes 1, 2, and 4. |
| 72 | +</pre> |
| 73 | +</div> |
| 74 | +</div> |
| 75 | + |
| 76 | +<p> </p> |
| 77 | + |
| 78 | +<p><strong>Note:</strong></p> |
| 79 | + |
| 80 | +<ol> |
| 81 | + <li><code>1 <= A.length <= 20000</code></li> |
| 82 | + <li><code>0 <= A[i] < 100000</code></li> |
| 83 | +</ol> |
| 84 | + |
| 85 | + |
| 86 | +### Related Topics |
| 87 | + [[Stack](https://github.com/openset/leetcode/tree/master/tag/stack/README.md)] |
| 88 | + [[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] |
0 commit comments