Leetcode55 跳跃游戏 动态规划和贪心算法

给定一个非负整数数组,你需要判断能否从第一个位置到达最后一个位置。动态规划方法通过自底向上计算每个位置的可达性,而贪心策略尝试每次都跳尽可能远的距离。例如,对于输入[2,3,1,1,4],可以使用贪心策略到达终点;但对于[3,2,1,0,4],则无法到达终点。" 110090389,9214246,JAVA实现环形队列:数组模拟及操作详解,"['数据结构', '算法', 'Java', '队列']
摘要由CSDN通过智能技术生成

题目描述

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

判断你是否能够到达最后一个位置。

示例 1:

输入: [2,3,1,1,4]
输出: true
解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置。
示例 2:

输入: [3,2,1,0,4]
输出: false
解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。

解法1

1.1 Top Down

思路:从头开始搜索,用一个memo来记录每个对应index是否为通路,{1:通路;-1:非通路;0:未知}. 使用递归查找,如果index能到达的路径中存在一条通路,就在memo中将这个index的值设为1;反之则为-1

class Solution {
   public boolean canJump(int[] nums) {
        boolean isReachable;
        int[] memo = new int[nums.length];
        Arrays.fill(memo, 0);
        memo[memo.length - 1] = 1;
        if (nums.length < 2) isReachable = true;
        else
        {
            isReachable = isWithinReach(memo, nums, 0);
        }
        return  isReachable;
    }
    public boolean isWithinReach(int[] memo, int[] nums, int currIndex)
    {
        if (memo[currIndex] == 1) return true;
        else if (memo[currIndex] == -1) return false;
        else
        {
            int MaxReach = Math.min(currIndex + nums[currIndex], nums.length - 1);
            for (int i = currIndex + 1; i <= MaxReach; i++)
            {
                boolean jumpResult = isWithinReach(memo, nums, i);
                if (jumpResult)
                {
                    memo[currIndex] = 1;
                    return true;
                }
            }
            memo[currIndex] = -1;
            return false;
        }
    }
}

1.2 Buttom Up

思路:延续1.1的memo法,实际做法就是把递归改成一个while循环

class Solution {
   public boolean canJump(int[] nums) {
        boolean isReachable = false;
        int[] memo = new int[nums.length];
        Arrays.fill(memo, 0);
        memo[memo.length - 1] = 1;
        if (nums.length < 2) isReachable = true;
        else
        {
            for (int i = nums.length - 2; i >= 0; i--)
            {
                int MaxReach = Math.min(i + nums[i], nums.length - 1);
                for (int j = i + 1; j <= MaxReach; j++)
                {
                    if (memo[j] == 1)
                    {
                        memo[i] = 1;
                        break;
                    }
                }
            }
            if (memo[0] == 1) isReachable = true;
        }
        return  isReachable;
    }
}

解法2 贪心算法

思路:解法1中最优的buttom up算法用了两个循环,还用了一个和nums一样长度的memo数组来记录。贪心算法有点类似最大子序和中的在线处理算法,只用一个循环,用一个int变量来记录。

class Solution {
   public boolean canJump(int[] nums) {
        int MaxJumpStep = 0;
        for (int i = 0; i < nums.length; i++)
        {
            if (i > MaxJumpStep) return false;
            if (MaxJumpStep >= nums.length - 1) return true;
            MaxJumpStep = Math.max(MaxJumpStep, i + nums[i]);
        }
        return true;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值