Maximum Number of Jumps to Reach the Last Index

mail@pastecode.io avatar
unknown
c_cpp
a year ago
527 B
6
Indexable
Never
class Solution {
public:
    
    
    int maximumJumps(vector<int>& nums, int target) {
        
        int dp[nums.size()];
        
        memset(dp, 0, sizeof(dp));

        for(int i=0; i<nums.size(); ++i){
            if(dp[i]>0 || i==0)
            for(int j=i+1; j<nums.size(); ++j){
                if(abs(nums[j]-nums[i])<=target)
                    dp[j] = max(dp[j], dp[i]+1);
            }
        }
        
        return dp[nums.size()-1] == 0 ? -1 : dp[nums.size()-1];
        
    }
};