Leetcode [Hard] – “Odd Even Jump” in C++

While I was doing some leetcode problems, one interesting question drew my attention. I spent quite some times on this problem, and ended up solving it with dynamic programming (dp).

This problem was particularly interesting and made me write a blog about it because it’s quite a representative dp problem and also uses functions of map that I wasn’t too familiar with, upper_bound and lower_bound. While I was working on this problem, I learned many of the awesome tricks and techniques for solving such kind of dp problems and acheive near-optimal efficiency.

Here is the description of the problem:

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.

At first glance, this problem is quite confusing because of the way that it explains itself, as well as some of the conditions aren’t told in the text. But a general sense for this problem is you start from some index i, and i is a good starting index if you can somehow reach the last element in the array by having a series of odd and even jumps. The condition for performing an odd and even jump is described above. We find and return the number of good starting index.

Observation

  1. Notice for every given input array with size > 0, if we set starting index to array.size()-1, in other words, last element, then it will always jump to the end since its already at the end. So last element is always a good starting index.
  2. Second to last element always depends on the last element since its first jump is a jump onto last element. This means that if the jump from second to last element is a valid jump, i.e., last element >= second to last element, then we already know its a good starting index since we know forsure last element can always reach to the end.
  3. An inductive result drived from the second observation tells us that as we go backward from the last element in the array, each jump it makes uses a result that we have already computed, thus, no need to redo all the unnecessary jumping again. And this is where Dynamic Programming comes into play.

Dynamic Programming

Dynamic programming is an algorithmic optimization for plain recursion. In many of the problem, we keep making recursive calls for same inputs, but wiith dynamic programming solution, results for some of the inputs are stored so that when they are being used again, we don’t have to re-compute them. To be honest, I am no expert in dynamic programming, it is very much of a shortcoming of mine, but I will post more interesting leetcode dp problem and their solutions to make myself more confortable with it.

class Solution {
public:
    int oddEvenJumps(vector<int>& A) {
        int size = A.size();
        int to_return = 1;
        
        vector<int> high(size);
        vector<int> low(size);
        
        high[size-1] = 1;
        low[size-1] = 1;
        
        map<int, int> m;
        m[A[size-1]] = size-1;
        
        for (auto i = size-2; i>=0; --i) {
            auto hi = m.lower_bound(A[i]), lo = m.upper_bound(A[i]);
            
            if (hi != m.end()) high[i] = low[hi->second];
            if (lo != m.begin()) low[i] = high[(--lo)->second];
            
            if (high[i]) to_return++;
            
            m[A[i]] = i;
        }
        
        return to_return;
    }
};

Above is the solution I have, the credit of the idea goes to https://leetcode.com/lee215.

On Line 5 I created the result and initialized it to 1 since we already know for any given array size > 0, last element is always a good starting index. Line 6 and line 7 I declared two vectors high and low of size equal to the size of array. They of type int since there is no primitive datatype boolean in C++, and the value at index j of both vectors represents a is A[j] a good starting index or not. It goes without being said, the last value of vector high and low is 1 (1 means good starting index, 0 means not) based on my first observation, which is what I did on line 10 and line 11.

Notice on line 13, I created a map and inserted the last element of the input array with its value as key and index as value in the following line. The reason why map is being used is that in C++, map stores its value as a binary tree structure and sorts its data based on its key value. And also I can used functions upper_bound and lower_bound. I will explain those those functions do later. Map will be used for checking is there a valid jump for a certain index.

In the forloop between line 16 and line 25 is where the magic happens. Starting from the second to last element (since check the last element doesn’t need to be checked), I find a hi and a lo. hi variable stores the key-value pair from map that is >= element we are checking, and lo stores the first element that goes after the element we are checking. Ok, so hi stores the next element we can jump to with a odd jump, but what the hell is lo? Well, interestingly, upper_bound doesn’t give you an element that is actually upper bounded by the given value for some reason, it gives the first element that is greater than the input value. For example, if we have an array [1, 2, 3, 4, 5], upper_bound(3) gives you 4 instead of 3, so in order to get the value I need, I have to decrement it to get the iterator that is <= the input A[i] (Line 20).

On line 22, to increment to_return if high[i], in other words, if the odd jump from index i is takes us to the end. Then finally on line 24, we insert a new key-value pair into map.

Takeaways

  • For problems that recursively re-computing the result from the same input, we can always use dp. In this case, we have two vectors high and low that records if each starting index is a good starting index or not.
  • Map is always a good data structure if we want to continuely adding key-value pair and have them sorted along the way since both insert and erase takes O(log(n)). And its upper_bound and lower_bound functions is perfect for this particular problem.
  • Process an array in reverse order can sometimes give better performance.

Leave a comment