|
| 1 | +/** |
| 2 | + * 975. Odd Even Jump |
| 3 | + * https://leetcode.com/problems/odd-even-jump/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given an integer array arr. From some starting index, you can make a series of jumps. |
| 7 | + * The (1st, 3rd, 5th, ...) jumps in the series are called odd-numbered jumps, and the (2nd, |
| 8 | + * 4th, 6th, ...) jumps in the series are called even-numbered jumps. Note that the jumps are |
| 9 | + * numbered, not the indices. |
| 10 | + * |
| 11 | + * You may jump forward from index i to index j (with i < j) in the following way: |
| 12 | + * - During odd-numbered jumps (i.e., jumps 1, 3, 5, ...), you jump to the index j such that |
| 13 | + * arr[i] <= arr[j] and arr[j] is the smallest possible value. If there are multiple such |
| 14 | + * indices j, you can only jump to the smallest such index j. |
| 15 | + * - During even-numbered jumps (i.e., jumps 2, 4, 6, ...), you jump to the index j such that |
| 16 | + * arr[i] >= arr[j] and arr[j] is the largest possible value. If there are multiple such |
| 17 | + * indices j, you can only jump to the smallest such index j. |
| 18 | + * - It may be the case that for some index i, there are no legal jumps. |
| 19 | + * |
| 20 | + * A starting index is good if, starting from that index, you can reach the end of the array (index |
| 21 | + * arr.length - 1) by jumping some number of times (possibly 0 or more than once). |
| 22 | + * |
| 23 | + * Return the number of good starting indices. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number[]} arr |
| 28 | + * @return {number} |
| 29 | + */ |
| 30 | +var oddEvenJumps = function(arr) { |
| 31 | + const oddCanReach = new Array(arr.length).fill(false); |
| 32 | + const evenCanReach = new Array(arr.length).fill(false); |
| 33 | + oddCanReach[arr.length - 1] = true; |
| 34 | + evenCanReach[arr.length - 1] = true; |
| 35 | + let result = 1; |
| 36 | + |
| 37 | + const sortedIndices = Array.from({ length: arr.length }, (_, i) => i); |
| 38 | + sortedIndices.sort((a, b) => arr[a] - arr[b] || a - b); |
| 39 | + |
| 40 | + const oddNext = new Array(arr.length); |
| 41 | + const evenNext = new Array(arr.length); |
| 42 | + const stack = []; |
| 43 | + |
| 44 | + for (const i of sortedIndices) { |
| 45 | + while (stack.length && stack[stack.length - 1] < i) { |
| 46 | + oddNext[stack.pop()] = i; |
| 47 | + } |
| 48 | + stack.push(i); |
| 49 | + } |
| 50 | + |
| 51 | + sortedIndices.sort((a, b) => arr[b] - arr[a] || a - b); |
| 52 | + stack.length = 0; |
| 53 | + |
| 54 | + for (const i of sortedIndices) { |
| 55 | + while (stack.length && stack[stack.length - 1] < i) { |
| 56 | + evenNext[stack.pop()] = i; |
| 57 | + } |
| 58 | + stack.push(i); |
| 59 | + } |
| 60 | + |
| 61 | + for (let i = arr.length - 2; i >= 0; i--) { |
| 62 | + if (oddNext[i] !== undefined) { |
| 63 | + oddCanReach[i] = evenCanReach[oddNext[i]]; |
| 64 | + if (oddCanReach[i]) result++; |
| 65 | + } |
| 66 | + if (evenNext[i] !== undefined) { |
| 67 | + evenCanReach[i] = oddCanReach[evenNext[i]]; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return result; |
| 72 | +}; |
0 commit comments