leetcode_57 Jump Game V

161 篇文章 0 订阅

题目:

Given an array of integers arr and an integer d. In one step you can jump from index i to index:

  • i + x where: i + x < arr.length and 0 < x <= d.
  • i - x where: i - x >= 0 and 0 < x <= d.

In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).

You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.

Notice that you can not jump outside of the array at any time.

Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.

Example 2:

Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.

Example 3:

Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies. 

Example 4:

Input: arr = [7,1,7,1,7,1], d = 2
Output: 2

Example 5:

Input: arr = [66], d = 1
Output: 1

Constraints:

  • 1 <= arr.length <= 1000
  • 1 <= arr[i] <= 10^5
  • 1 <= d <= arr.length

开始进行BFS步数不好计算,标记的是每层,而需要的是深度后来采用迭代 DFS + DP,使用DP数组记录每个index的最多经过的点。

    dp[index] 如果计算过就不用重复计算,

    M 每个未计算的index的初始值为0,dfs 每加深一层,就加1,最终取 2d种情况的最大值

    以index为中心,在[index-i,index+i]  0 < i <= d  中,对应值一旦大于index对应值,i不再增大,停止index继续增加或减小

class Solution {
public:
    int dfs(vector<int>& arr,vector<int> &dp,int d,int index)
    {
        if(dp[index]!= -1) return dp[index];
        int step = 1;
        int M = 0;
        int val = arr[index];
        
        for(int i = 1; i <= d; i++)
        {
            if(index + i >= arr.size()) break;
            if(arr[index + i] >= val) break;
            M = max(M,dfs(arr,dp,d,index + i));
        }
        for(int i = 1; i <= d; i++)
        {
            if(index - i < 0) break;
            if(arr[index - i] >= val) break;
            M = max(M,dfs(arr,dp,d,index - i));
        }
        dp[index] = step + M;
        return dp[index];
    }
    int maxJumps(vector<int>& arr, int d) {
        int n = arr.size();
        vector<int> dp(n,-1);
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            ans = max(ans,dfs(arr,dp,d,i));
        }
        return ans;
    }
};

 DFS+DP的精简写法:

int memo[100000];
class Solution {
public:
    int d;
    int dp(vector<int>& arr,int index)
    {
        if(memo[index]!=-1)                                       //Return the cached value if exists.
            return memo[index];
        memo[index]=0;
        for(int i=index+1;i<arr.size()&&arr[i]<arr[index]&&i<=index+d;i++)     //Check the indices on the right while storing Max.
            memo[index]=max(memo[index],1+dp(arr,i));
        for(int i=index-1;i>=0&&arr[i]<arr[index]&&i>=index-d;i--)                 //Check the indices on the left while storing Max.
            memo[index]=max(memo[index],1+dp(arr,i));
        return memo[index];                                                         //Return the maximum of all checked indices.
    }
    int maxJumps(vector<int>& arr, int d) 
    {
        memset(memo,-1,sizeof memo);
        int result=0;
        this->d=d;
        for(int i=0;i<arr.size();i++)                     //Check for all indices as starting point.
            result=max(result,1+dp(arr,i));
        return result;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值