Algorithm


Problem Name: 975. Odd Even Jump

You are given an integer array arr. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called odd-numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even-numbered jumps. Note that the jumps are numbered, not the indices.

You may jump forward from index i to index j (with i < j) in the following way:

  • During odd-numbered jumps (i.e., jumps 1, 3, 5, ...), you jump to the index j such that arr[i] <= arr[j] and arr[j] is the smallest possible value. If there are multiple such indices j, you can only jump to the smallest such index j.
  • During even-numbered jumps (i.e., jumps 2, 4, 6, ...), you jump to the index j such that arr[i] >= arr[j] and arr[j] is the largest possible value. If there are multiple such indices j, you can only jump to the smallest such index j.
  • It may be the case that for some index i, there are no legal jumps.

A starting index is good if, starting from that index, you can reach the end of the array (index arr.length - 1) by jumping some number of times (possibly 0 or more than once).

Return the number of good starting indices.

 

Example 1:

Input: arr = [10,13,12,14,15]
Output: 2
Explanation: 
From starting index i = 0, we can make our 1st jump to i = 2 (since arr[2] is the smallest among arr[1], arr[2], arr[3], arr[4] that is greater or equal to arr[0]), then we cannot jump any more.
From starting index i = 1 and i = 2, we can make our 1st jump to i = 3, then we cannot jump any more.
From starting index i = 3, we can make our 1st jump to i = 4, so we have reached the end.
From starting index i = 4, we have reached the end already.
In total, there are 2 different starting indices i = 3 and i = 4, where we can reach the end with some number of
jumps.

Example 2:

Input: arr = [2,3,1,1,4]
Output: 3
Explanation: 
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3
During our 3rd jump (odd-numbered), we jump from i = 2 to i = 3 because arr[3] is the smallest value in [arr[3], arr[4]] that is greater than or equal to arr[2].
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good.
In a similar manner, we can deduce that:
From starting index i = 1, we jump to i = 4, so we reach the end.
From starting index i = 2, we jump to i = 3, and then we can't jump anymore.
From starting index i = 3, we jump to i = 4, so we reach the end.
From starting index i = 4, we are already at the end.
In total, there are 3 different starting indices i = 1, i = 3, and i = 4, where we can reach the end with some
number of jumps.

Example 3:

Input: arr = [5,1,3,4,2]
Output: 3
Explanation: We can reach the end from starting indices 1, 2, and 4.

 

Constraints:

  • 1 <= arr.length <= 2 * 104
  • 0 <= arr[i] < 105

Code Examples

#1 Code Example with C Programming

Code - C Programming


int smallest_among_greaters(int k, int *map, int sz) {
    for (int i = k; i  <  sz; i ++) {
        if (map[i] != 0) return map[i];
    }
    return 0;
}
int greatest_among_smallers(int k, int *map, int sz) {
    for (int i = k; i >= 0; i --) {
        if (map[i] != 0) return map[i];
    }
    return 0;
}
int oddEvenJumps(int* A, int ASize){
    // if jump can to to a high or low number on current index
    bool hi[20000] = { false }, lo[20000] = { false };
    
    // index of a number is on
    int map[100000] = { 0 };
#define sz (sizeof(map) / sizeof(map[0]))
    
    int i, h, l, ans;
    
    hi[ASize - 1] = lo[ASize - 1] = true;
    
    map[A[ASize - 1]] = ASize;  // index + 1
    
    ans = 1;
    
    for (i = ASize - 2; i >= 0; i --) {
        h = smallest_among_greaters(A[i], map, sz);
        l = greatest_among_smallers(A[i], map, sz);
        if (h && lo[h - 1]) { hi[i] = true; ans ++; }
        if (l && hi[l - 1]) lo[i] = true;
        map[A[i]] = i + 1;
    }
    
    return ans;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [10,13,12,14,15]

Output

x
+
cmd
2

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int oddEvenJumps(int[] A) {
    int n = A.length;
    if (n  < = 1) {
      return n;
    }
    boolean[] odd = new boolean[n];
    boolean[] even = new boolean[n];
    odd[n - 1] = even[n - 1] = true;
    TreeMap < Integer, Integer> map = new TreeMap<>();
    map.put(A[n - 1], n - 1);
    int count = 0;
    for (int i = n - 2; i >= 0; i--) {
      int val = A[i];
      if (map.containsKey(val)) {
        odd[i] = even[map.get(val)];
        even[i] = odd[map.get(val)];
      }
      else {
        Integer lower = map.lowerKey(val);
        Integer higher = map.higherKey(val);
        if (lower != null) {
          even[i] = odd[map.get(lower)];
        }
        if (higher != null) {
          odd[i] = even[map.get(higher)];
        }
      }
      map.put(val, i);
    }
    for (boolean b : odd) {
      count += b ? 1 : 0;
    }
    return count;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [10,13,12,14,15]

Output

x
+
cmd
2

#3 Code Example with Javascript Programming

Code - Javascript Programming


const oddEvenJumps = function (A) {
  // Creates an array with ONLY the indices of the sorted array
  let sorted = A.map((el, idx) => idx).sort((a, b) => A[a] - A[b] || a - b)
  // Create an array of '-1's of the same array length for odd and even jumps
  let oddJumps = new Array(A.length).fill(-1)
  let evenJumps = new Array(A.length).fill(-1)
  // Create an empty stack
  let stack = []
  // Loop the the sorted array of the indices
  for (let i of sorted) {
    // Loops as long the stack is full OR if the index is greater than the the last index of the stack
    while (stack.length && i > stack[stack.length - 1]) {
      // Pops the index from the stack and place and add the 'i' index in sortedJumps
      oddJumps[stack.pop()] = i
    }
    // Pushes the index onto the stack
    stack.push(i)
  }
  // Empty the stack
  stack = []
  // Reverses the sorted index array
  let reverseSorted = sorted.sort((a, b) => A[b] - A[a] || a - b)
  // Does the exact thing but for even jumps
  for (let i of reverseSorted) {
    while (stack.length && i > stack[stack.length - 1]) {
      evenJumps[stack.pop()] = i
    }
    stack.push(i)
  }
  // Starts the count at 0
  let count = 1
  // Creates a boolean array of false elements for even and odd ends
  let oddEnd = new Array(A.length).fill(false)
  let evenEnd = new Array(A.length).fill(false)
  // Switches the end of each array to true
  oddEnd[A.length - 1] = true
  evenEnd[A.length - 1] = true
  // Loops through the array, starting from the 2nd from the right (since we do not need to worry about       the last index)
  for (let i = A.length - 2; i >= 0; --i) {
    // If even jumps does
    if (evenJumps[i] !== -1 && oddEnd[evenJumps[i]]) evenEnd[i] = true
    if (oddJumps[i] !== -1 && evenEnd[oddJumps[i]]) {
      oddEnd[i] = true
      count++
    }
  }
  return count
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [2,3,1,1,4]

Output

x
+
cmd
3

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def oddEvenJumps(self, A: List[int]) -> int:
        n = len(A)
        next_higher, next_lower = [0] * n, [0] * n

        stack = []
        for a, i in sorted([a, i] for i, a in enumerate(A)):
            while stack and stack[-1] < i:
                next_higher[stack.pop()] = i
            stack.append(i)

        stack = []
        for a, i in sorted([-a, i] for i, a in enumerate(A)):
            while stack and stack[-1] < i:
                next_lower[stack.pop()] = i
            stack.append(i)

        higher, lower = [0] * n, [0] * n
        higher[-1] = lower[-1] = 1
        for i in range(n - 1)[::-1]:
            higher[i] = lower[next_higher[i]]
            lower[i] = higher[next_lower[i]]
        return sum(higher)
        
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [2,3,1,1,4]

Output

x
+
cmd
3

#5 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _0975_OddEvenJump
    {
        public int OddEvenJumps(int[] A)
        {
            var length = A.Length;
            var oddPosibility = new bool[length];
            var evenPosibility = new bool[length];
            oddPosibility[length - 1] = true;
            evenPosibility[length - 1] = true;

            var map = new SortedList < int, int>
            {
                { A[length - 1], length - 1 }
            };
            var result = 1;
            for (int i = length - 2; i >= 0; i--)
            {
                var existed = map.TryGetValue(A[i], out int index);
                map[A[i]] = i;

                if (existed)
                {
                    oddPosibility[i] = evenPosibility[index];
                    evenPosibility[i] = oddPosibility[index];
                }
                else
                {
                    index = map.IndexOfKey(A[i]);
                    if (index != map.Count - 1)
                        oddPosibility[i] = evenPosibility[map.Values[index + 1]];
                    if (index != 0)
                        evenPosibility[i] = oddPosibility[map.Values[index - 1]];
                }

                if (oddPosibility[i])
                    result++;
            }

            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [5,1,3,4,2]

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#974 Leetcode Subarray Sums Divisible by K Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#976 Leetcode Largest Perimeter Triangle Solution in C, C++, Java, JavaScript, Python, C# Leetcode