LC 1340. Jump Game V

link

LC 1340. Jump Game V

 

 1.dfs+memo:

class Solution {
public:
    int n;
    int maxJumps(vector<int>& arr, int d) {
        n=arr.size();
        vector<int> memo(n,-1);
        int res=0;
       
        for(int i=0;i<n;++i){
            int tmp=dfs(i,arr,d,memo);
            res=max(res,tmp);
        }
        return res;
    }
    
    int dfs(int idx, vector<int>& arr, int d,vector<int> &memo){
        if(memo[idx]!=-1) return memo[idx];
        int cnt=1;
        for(int dis=1;idx+dis<n && dis<=d;++dis){
            if(arr[idx+dis]>=arr[idx]) break;
            cnt=max(cnt,1+dfs(idx+dis,arr,d,memo));
        }
       
        for(int dis=1;idx-dis>=0 && dis<=d;++dis){
            if(arr[idx-dis]>=arr[idx]) break;
            cnt=max(cnt,1+dfs(idx-dis,arr,d,memo));
        }
        return memo[idx]=cnt;
    }
};

 

2. segment tree + down-up dp:

class Solution {
public:
    int n;
    int seg[1001<<2];
    
    int maxJumps(vector<int>& arr, int d) {
        n=arr.size();
        vector<int> rightB(n);
        vector<int> leftB(n);
        stack<int> stk;
        for(int i=0;i<n;i++){
            while(!stk.empty() && arr[i]>=arr[stk.top()]){
                int t=stk.top();
                stk.pop();
                rightB[t]=min(i-1,t+d);
            }
            stk.push(i);
        }
        while(!stk.empty()){
            int t=stk.top();
            stk.pop();
            rightB[t]=min(n-1,t+d);
        }
        
        for(int i=n-1;i>=0;i--){
            while(!stk.empty() && arr[i]>=arr[stk.top()]){
                int t=stk.top();
                stk.pop();
                leftB[t]=max(i+1,t-d);
            }
            stk.push(i);
        }
        while(!stk.empty()){
            int t=stk.top();
            stk.pop();
            leftB[t]=max(0,t-d);
        }
        vector<int> index;
        for(int i=0;i<n;i++) index.push_back(i);
        
        sort(index.begin(),index.end(),[&](int i1, int i2){
            return arr[i1]<arr[i2];
        });
        int res=0;
        for(int i:index){
            int pre=0;
            if(i-1>=leftB[i]){
                pre=query(1,1,n,leftB[i]+1,i);
            }
            if(i+1<=rightB[i]){
                pre=max(pre,query(1,1,n,i+2,rightB[i]+1));
            }
            int tmp=1+pre;
            res=max(res,tmp);
            update(i+1,1,1,n,tmp);
        }
        return res;
    }
    
    void update(int uidx, int idx, int left, int right,int val){
        if(uidx<left || uidx>right) return;
        if(left==right){
            seg[idx]=val;
            return;
        }
        
        int mid=left+(right-left)/2;
        update(uidx,idx<<1,left,mid,val);
        update(uidx,idx<<1|1,mid+1,right,val);
        seg[idx]=max(seg[idx<<1],seg[idx<<1|1]);
    }
    
    int query(int idx, int left, int right, int qleft, int qright){
        if(qleft>right || qright<left) return 0;
        if(qleft<=left && qright>=right) return seg[idx];
        
        int mid=left+(right-left)/2;
        int l=query(idx<<1,left,mid,qleft,qright);
        int r=query(idx<<1|1,mid+1,right,qleft,qright);
        return max(l,r);
    }
    
   
};

 

上一篇:2020 力扣杯!Code Your Future 春季全国编程大赛 个人赛


下一篇:CTF-PWN-house-of-orange (unsortedbin attack+IO_FILE文件结构利用)