LeetCode 975. Parity hop -- set+map + dynamic programming

  1. Parity jump

Given an integer array A, you can jump A certain number of times from A starting index. In the process of your jump, the 1st, 3rd, 5th... Jumps are called odd jumps, while the 2nd, 4th, 6th... Jumps are called even jumps.

You can jump back from index i to index j (where i < j) as follows:

When performing odd jumps (e.g., 1st, 3rd, 5th)... You will jump to the index j,bring A[i] <= A[j],A[j] Is the lowest possible value. If there are multiple such indexes j,You can only jump to the minimum index that meets the requirements j Come on.
In even jumps (e.g., 2nd, 4th, 6th)... You will jump to the index j,bring A[i] >= A[j],A[j] Is the maximum possible value. If there are multiple such indexes j,You can only jump to the minimum index that meets the requirements j Come on.
(For some indexes i,It may not be possible to make the required jump.)

If you jump from an index a certain number of times (possibly 0 or more), you can reach the end of the array (index A.length - 1), then the index will be considered a good starting index.

Returns the number of good starting indexes.

Example 1:

Input: [10,13,12,14,15]
Output: 2
Explanation:
Starting from the starting index i = 0, we can jump to i = 2 (because A[2] is the minimum value greater than or equal to A[0] in A[1], A[2], A[3], A[4]), and then we can't continue to jump.
Starting from the starting indexes i = 1 and i = 2, we can jump to i = 3, and then we can't continue to jump.
Starting from the starting index i = 3, we can jump to i = 4 and reach the end of the array.
Starting from the starting index i = 4, we have reached the end of the array.
In short, we can start from two different starting indexes (i = 3, i = 4) and reach the end of the array through a certain number of jumps.

Example 2:

Input: [2,3,1,1,4]
Output: 3
Explanation:
Starting from the starting index i=0, we can jump to i = 1, i = 2, i = 3:

In our first jump (odd number), we first jump to i = 1, because A[1] is the minimum value greater than or equal to A[0] in (A[1], A[2], A[3], A[4]).

In our second jump (even number), we jump from i = 1 to i = 2 because A[2] is (A[2], A[3], A[4]) less than or equal to the maximum of A[1]. A[3] is also the largest value, but 2 is a small index, so we can only jump to i = 2, not i = 3.

In our third jump (odd), we jump from i = 2 to i = 3 because A[3] is the minimum value greater than or equal to A[2] in (A[3], A[4]).

We can't jump from i = 3 to i = 4, so the starting index i = 0 is not a good starting index.

Similarly, we can infer that:
Starting from the starting index i = 1, we jump to i = 4, so we reach the end of the array.
Starting from the starting index i = 2, we jump to i = 3, and then we can't jump any more.
Starting from the starting index i = 3, we jump to i = 4, so we reach the end of the array.
Starting from the starting index i = 4, we have reached the end of the array.
In short, we can start from three different starting indexes (i = 1, i = 3, i = 4) and reach the end of the array through a certain number of jumps.

Example 3:

Input: [5,1,3,4,2]
Output: 3
Explanation:
We can start from the start index 1, 2, 4 to the end of the array.

Tips:

1 <= A.length <= 20000
0 <= A[i] < 100000

Problem solution

First, the meaning of the question is that starting from a certain position x, you can only jump to the interval [x+1,n], and odd jumps can only jump to numbers greater than or equal to arr[x]. If there are multiple numbers greater than or equal to arr[x], find the smallest one. If there are multiple identical ones, find the one with the smallest subscript. Conversely, even jumps are easier to understand.

Direct reverse traversal, set stores the query, speeds up the query, and then marks the position of each element with map. Then, using the idea of dynamic programming, dp[x][0] indicates whether the odd hop from position x can arrive, and dp[x][1] indicates whether the even hop from position x can arrive.

AC code

class Solution {
public:
    set<int>q;
    map<int,int>pos;
    bool dp[20005][2];//dp[x][0] indicates jumping from position x odd number, dp[x][1] indicates jumping from position x even number
    int oddEvenJumps(vector<int>& arr) 
    {
        memset(dp,0,sizeof(dp));
        dp[arr.size()-1][0] = dp[arr.size()-1][1] = 1;
        q.insert(arr[arr.size()-1]);
        pos[arr[arr.size()-1]] = arr.size()-1;
        set<int>::iterator iter;
        for(int i=arr.size()-2;i>=0;i--)
        {
            iter = q.lower_bound(arr[i]);//Find greater than or equal to, corresponding to odd hop
            if(iter!=q.end())
            {
                dp[i][0] = dp[pos[*iter]][1];
                //cout<<arr[i]<<" "<<*iter<<" "<<dp[i][0]<<endl;
            }
            iter = q.upper_bound(arr[i]);//Find greater than, corresponding to even jump
            if(iter!=q.begin())
            {
                iter--;//Equal to or less than
                dp[i][1] = dp[pos[*iter]][0];
                //cout<<arr[i]<<" "<<*iter<<" "<<dp[i][1]<<endl;
            }
            
            q.insert(arr[i]);
            pos[arr[i]] = i;//Update to minimum subscript
        }
        int res=0;
        for(int i=0;i<arr.size();i++)
        {
            if(dp[i][0]==true)//You can only jump odd at first
            res++;
        }
        return res;
    }
};

Tags: C leetcode Dynamic Programming

Posted on Sun, 19 Sep 2021 18:39:42 -0400 by 3rve