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 &lt; 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] &lt;= 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] &gt;= 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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= arr.length &lt;= 2 * 104
	0 &lt;= arr[i] &lt; 105


class Solution:
    def oddEvenJumps(self, arr: List[int]) -> int:
        
        l = len(arr)
        res = [False]*l
        res[-1] = True
        
        # for odd jump: for i, get next larger one 
        odd_next = [i for i in range(l)]
        stack = [] # mono inc for ind
        for n, i in sorted([(arr[i], i) for i in range(l)]):
            while stack and stack[-1]<i:
                odd_next[stack.pop()] = i
            stack.append(i)
                
        # for even jump: for i, get next smaller one 
        even_next = [i for i in range(l)]
        stack = [] # mono inc for ind
        for n, i in sorted([(-arr[i], i) for i in range(l)]):
            while stack and stack[-1]<i:
                even_next[stack.pop()] = i
            stack.append(i)
            
        # dp 
        @lru_cache(None)
        def dp(i, odd):
            if i == l-1:
                return True
            ind = odd_next[i] if odd else even_next[i]
            
            if ind == i:
                return False
            
            return dp(ind, not odd)
        
        for i in range(l-1):
            res[i] = dp(i, True)
        return sum(res)
class Solution {
    public int oddEvenJumps(int[] arr) {

        int len = arr.length;
        int minjmp[] = new int[len];
        int maxjmp[] = new int[len];

        TreeMap<Integer, Integer> map = new TreeMap<>();
        int evjmp, oddjmp;
        for(int i = len-1; i>=0; i--)
        {
            Integer minpos = map.floorKey(arr[i]);
            evjmp = (minpos != null)?map.get(minpos):len; //default len, to show not possible

            if(evjmp != len && (evjmp == len-1 || maxjmp[evjmp] == len-1))
                evjmp = len-1; //check the last pos reachability

            Integer maxpos = map.ceilingKey(arr[i]);
            oddjmp = (maxpos != null) ? map.get(maxpos):len;

            if(oddjmp != len && (oddjmp == len-1 || minjmp[oddjmp] == len-1))
                oddjmp = len-1;//check the last pos reachability

            minjmp[i] = evjmp; //specify possible jump path, if not possible assign len
            maxjmp[i] = oddjmp;//specify possible jump path, if not possible assign len

            map.put(arr[i],i); //put the current index
        }

        int res = 0;

        for(int i = 0; i< len-1; i++) {

            if(maxjmp[i] == len-1)
                res++;
        }

        return res+1; //since last position will always be the answer
    }

}
class Solution {
private:
    void tryOddJump(const int idx,
                    const int val,
                    std::map<int, int>& visitedNode,
                    vector<int>& oddJumpsDP,
                    vector<int>& evenJumpsDP) {
        // looking for equal or bigger element and it's going to be smallest posible
        auto jumpPoint = visitedNode.lower_bound(val);
        // If from jumpPoint exists even jump further, then odd jump will have some value
        if(jumpPoint != visitedNode.end()) oddJumpsDP[idx] = evenJumpsDP[jumpPoint->second];
    }
    
    void tryEvenJump(const int idx,
                     const int val,
                     std::map<int, int>& visitedNode,
                     vector<int>& oddJumpsDP,
                     vector<int>& evenJumpsDP) {
        //looking for strictly bigger element because then I can be sure that previous will be equal or less that val for sure
        auto jumpPoint = visitedNode.upper_bound(val);
        // if it's first element, then there is no equal or less option, so no even jump possible
        if(jumpPoint != visitedNode.begin()) 
            evenJumpsDP[idx] = oddJumpsDP[std::prev(jumpPoint)->second]; // (jumpPoint-1) check if further odd jump is possible from largest element that is <= val 
    }
public:
    int oddEvenJumps(vector<int>& arr) {
        const size_t n = arr.size();
        vector<int> oddJumpsDP(n, 0);
        vector<int> evenJumpsDP(n, 0);
        oddJumpsDP[n-1] = 1;
        evenJumpsDP[n-1] = 1;
        std::map<int, int> visitedNode;
        visitedNode[arr[n-1]] = n-1;
        
        /*
        Idea is to move backward and keep track of posibility of odd and even jumps from each cell.
        This way at any jump we could check if further jumps is possible.
        Example 1:
                          v
            arr = [10,13,12,14,15]
            odd = [ -, -, 0, 1, 1]
            even= [ -, -, -, 0, 1]
            
            Let's say we curently have answers for 14 and 15 and trying to work on 12:
                tryOddJump() -> will give us 14 as only possible odd jump(arr[i] <= arr[j] and arr[j] is the smallest possible value), 
				but there is no EVEN jump from 14 further, 
				so we cache odd jump for 12 as 0 as well. 
				That's why I called thouse variables as oddJumpsDP/evenJumpsDP,
				they are caches for subproblems that helps in later computations.
				Without them we would need to check entire further path starting from each cell and it's not efficient.
        */
        int res = 1;
        for(int i=n-2; i>=0; --i) {
            tryOddJump(i, arr[i], visitedNode, oddJumpsDP, evenJumpsDP);
            tryEvenJump(i, arr[i], visitedNode, oddJumpsDP, evenJumpsDP);
            // we alwayse start from 1st jump that is odd jump, so we can add index to the result if odd jump from it is possible
            if(oddJumpsDP[i] == 1) ++res;
            visitedNode[arr[i]] = i;
        }
        
        return res;
    }
};
/**
 * @param {number[]} arr
 * @return {number}
 */
var oddEvenJumps = function(arr) {
    let result = 0;
    const len = arr.length
    const map = {} // { value: index }
    const memo = new Array(2).fill(null).map(i => new Array(len).fill(false))
    
    // Build a BST to maintain logN search/Insert
    function treeNode(value) {
        this.val = value;
        this.left = null;
        this.right = null;
    }
    
    const root = new TreeNode(arr[len - 1])
    
    function InsertToBST(val) {
        function dfs(node){
            if(node.val < val) {
                if(!node.right) node.right = new TreeNode(val);
                else dfs(node.right);
            }
            if(node.val > val) {
                if(!node.left) node.left = new TreeNode(val);
                else dfs(node.left);
            }
        }
        
        dfs(root);
    }
    
    
    
    
    memo[0][len - 1] = memo[1][len - 1] = true;
    map[arr[len - 1]] = len - 1;
    
    //memo[0] odd jumps
    //memo[1] even jumps
    
    function getNextBig(base) { // odd jump
        if(map[base] !== undefined) {
            return base
        }
        let result = Infinity
        function dfs(node) {
            if(!node) return;
            
            if(node.val > base && node.val < result) {
                result = node.val;
                dfs(node.left)
            }
            
            if(node.val < base) {
                dfs(node.right);
            }
        }
        
        dfs(root)
        
        return result === Infinity ? false : result;
    }
    
    function getNextSmall(base) { //even jump
        if(map[base] !== undefined) {
            return base
        }
        let result = -Infinity
        function dfs(node) {
            if(!node) return;
            
            if(node.val < base && node.val > result) {
                result = node.val;
                dfs(node.right)
            }
            
            if(node.val > base) {
                dfs(node.left);
            }
        }
        
        dfs(root)
        
        return result === -Infinity ? false : result;
    }
    
    for(let i = len - 2; i >= 0; i--) {
        let nextBig = getNextBig(arr[i])
        let nextSmall = getNextSmall(arr[i])
        
        memo[0][i] = nextBig ? memo[1][map[nextBig]] : nextBig;
        memo[1][i] = nextSmall ? memo[0][map[nextSmall]] : nextSmall;
        map[arr[i]] = i;
        InsertToBST(arr[i])
    }

    return memo[0].filter(boo => boo).length
    
};
Odd Even Jump