403、青蛙过河

  1. 403、青蛙过河
    1. 示例 1:
    2. 示例 2:
  2. 题解
    1. 1、暴力递归
    2. 优雅的暴力
    3. 3、记忆化递归
    4. 3、记忆化递归 + 二分搜索合适的跳跃位置
    5. 3、动态规划——自下而上

403、青蛙过河

一只青蛙想要过河。 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有)。 青蛙可以跳上石头,但是不可以跳入水中。

给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上)。 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2)。

如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1、k 或 k + 1个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。

请注意:

  • 石子的数量 ≥ 2 且 < 1100;
  • 每一个石子的位置序号都是一个非负整数,且其 < 231;
  • 第一个石子的位置永远是0。

示例 1:

[0,1,3,5,6,8,12,17]

总共有8个石子。
第一个石子处于序号为0的单元格的位置, 第二个石子处于序号为1的单元格的位置,
第三个石子在序号为3的单元格的位置, 以此定义整个数组...
最后一个石子处于序号为17的单元格的位置。

返回 true。即青蛙可以成功过河,按照如下方案跳跃: 
跳1个单位到第2块石子, 然后跳2个单位到第3块石子, 接着 
跳2个单位到第4块石子, 然后跳3个单位到第6块石子, 
跳4个单位到第7块石子, 最后,跳5个单位到第8个石子(即最后一块石子)。

示例 2:

[0,1,2,3,4,8,9,11]

返回 false。青蛙没有办法过河。 
这是因为第5和第6个石子之间的间距太大,没有可选的方案供青蛙跳跃过去。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/frog-jump
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

1、暴力递归

  • 代码
// java
class Solution {
    public boolean canCross(int[] stores) {
        return canCrassCore(stores,0,0);
    }

    private boolean canCrossCore(int[] stores,int index,int jumpSize) {
        for (int i = index + 1;i < stores.length;i++) {
            int gap = stores[i] - stores[index];
            // 满足下一次跳跃的条件
            if (gap >= jumpSize - 1 && gap <= jumpSize + 1) {
                // 递归调用
                if (canCroossCore(stores,i,gap)) {
                    return true;
                }
            }
        }
    }
}

优雅的暴力

有条件的递归,减少不必要的搜索,部分剪枝

public class Solution {
    public boolean canCross(int[] stones) {
        return can_Cross(stones, 0, 0);
    }
    // ind 开始下标
    public boolean can_Cross(int[] stones, int ind, int jumpsize) {
        if (ind == stones.length - 1) {
            return true;
        }
        // 二分查找目标值k
        int ind1 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize);
        // 满足条件并跳跃
        if (ind1 >= 0 && can_Cross(stones, ind1, jumpsize)) {
            return true;
        }
        // 二分查找目标值k-1
        int ind2 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize - 1);
         // 满足条件并跳跃
        if (ind2 >= 0 && can_Cross(stones, ind2, jumpsize - 1)) {
            return true;
        }
        // 二分查找目标值k+1
        int ind3 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize + 1);
         // 满足条件并跳跃
        if (ind3 >= 0 && can_Cross(stones, ind3, jumpsize + 1)) {
            return true;
        }
        return false;
    }
}

// 作者:LeetCode
// 链接:https://leetcode-cn.com/problems/frog-jump/solution/qing-wa-guo-he-by-leetcode/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3、记忆化递归

$memo[i][j]$ 表示是否可以从i处跳跃到j处

public class Solution {
    public boolean canCross(int[] stores) {
        int[][] memo = new int[stores.length][stores.length];
        for (int[] row : memo) {
            Arrays.fill(row,-1);
        }
        return canCrossCore(stores,0,0,memo) == 1;
        
    }
    public int canCrossCore(int[] stores,int index,int jumpSize,int[][] memo) {
        if (memo[index][jumpSize] >= 0) {
            return memo[index][jumpSize];
        }

        for (int i = index+1;i < stores.length;i++) {
            int gap = stores[i] - stores[index];
            if (gap >= jumpSize - 1 && gap <= jumpSize +1) {
                // 递归搜索
                if (canCrossCore(stores,i,gap,memo) == 1) {
                    memo[index][gap] = 1;// 存储中间值
                    return 1;
                }
            }
        }
        memo[index][jumpSize] = (index == stores.length - 1)? 1: 0;
        return memo[index][jumpSize];
    }
}

3、记忆化递归 + 二分搜索合适的跳跃位置

public class Solution {
    public boolean canCross(int[] stones) {
        int[][] memo = new int[stones.length][stones.length];
        for (int[] row : memo) {
            Arrays.fill(row, -1);
        }
        return can_Cross(stones, 0, 0, memo) == 1;
    }
    public int can_Cross(int[] stones, int ind, int jumpsize, int[][] memo) {
        if (memo[ind][jumpsize] >= 0) {
            return memo[ind][jumpsize];
        }
        int ind1 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize);
        if (ind1 >= 0 && can_Cross(stones, ind1, jumpsize, memo) == 1) {
            memo[ind][jumpsize] = 1;
            return 1;
        }
        int ind2 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize - 1);
        if (ind2 >= 0 && can_Cross(stones, ind2, jumpsize - 1, memo) == 1) {
            memo[ind][jumpsize - 1] = 1;
            return 1;
        }
        int ind3 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize + 1);
        if (ind3 >= 0 && can_Cross(stones, ind3, jumpsize + 1, memo) == 1) {
            memo[ind][jumpsize + 1] = 1;
            return 1;
        }
        memo[ind][jumpsize] = ((ind == stones.length - 1) ? 1 : 0);
        return memo[ind][jumpsize];
    }
}

// 作者:LeetCode
// 链接:https://leetcode-cn.com/problems/frog-jump/solution/qing-wa-guo-he-by-leetcode/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3、动态规划——自下而上

在动态规划方法中,我们会利用散列表 map,对于散列表中的 key:valuekey 表示当前石头的位置,value是一个包含 jumpsizejumpsize 的集合,其中每个 jumpsize 代表可以通过大小为 jumpysize 的一跳到达当前位置。

首先我们对散列表初始化,key 为所有石头的位置,除了位置 0 对应的 value 为包含一个值 0 的集合以外,其余都初始化为空集。

接下来,依次遍历每个位置上的石头。对于每个 currentPosition,遍历 value 中每个 jumpsize,判断位置 currentPosition + newjumpsize 是否存在于 map 中,对于每个 jumpsize,newjumpsize 分别为 jumpsize−1,jumpsize,jumpsize+1。如果找到了,就在对应的 valuevalue 集合里新增 newjumpsize。重复这个过程直到结束。

如果在结束的时候,最后一个位置对应的集合非空,那也就意味着我们可以到达终点,如果还是空集那就意味着不能到达终点。

链接:https://leetcode-cn.com/problems/frog-jump/solution/qing-wa-guo-he-by-leetcode/

public class Solution {
    public boolean canCross(int stores) {
        Map<Integer,Set<Integer>> map = new HashMap<>();
        // 为每一处的石块初始化,可能的跳跃集合
        for (int i = 0;i < stores.length;i++) {
            map.put(stores[i],new HashSet<Interger>());
        }
        map.get(0).add(0);
        // 遍历所有的石块,目标依次递增
        for (int i = 0;i < stores.length;i++) {
            // 遍历跳跃集合
            for (int k:map.get(stores[i])) {
                for (int step = k-1;step <= k+1;step++) {
                    // 有可以到达的石块
                    if (step > 0 && map.containsKey(stores[i]+step)) {
                        // 为到达的石块添加可能的k
                        map.get(stores[i]+step).add(step);
                    }
                }
            }
        }
        return map.get(stores[stores.length - 1]).size() > 0;
    }
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:403、青蛙过河

Count:1.7k

Author:攀登

Created At:2020-07-26, 00:19:44

Updated At:2024-06-15, 15:52:32

Url:http://jiafeimao-gjf.github.io/2020/07/26/403%E3%80%81%E9%9D%92%E8%9B%99%E8%BF%87%E6%B2%B3/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation