First let’s consider the case where max(hi+1,…,hj−1)<min(hi,hj). hi could be lower or higher than hj. There can be multiple i that satisfies the condition and we can observe the leftmost i is the first that hi≥hj, let’s denote this imin, other i between [imin,j] must satisfy that hi is the maximum value among [i,j−1]. This can be solved using monotonic stack. Assume we store the indices in the stack, when adding a new index j, all the indices that will be removed are a valid position to jump to j, so we can do dp and update the minimum number of moves. The time complexity is O(n).