[leetcode] 403. Frog Jump

news/2024/6/29 11:39:44

[链接描述]leetcode 题目。


有点类似于jump game, 只不过这里对步数有了隐形的要求,当前步数和之前步数有关。
如果我们用brute force的方法来解,每一步有三种可能,一共n个石头,时间复杂度就是O(3^n)。
这其中有很多步数是多余的,第i个石头无法从任何一个石头到达,那么我们应该立即中止搜寻。
还有一个石头可能由之前的多个石头到达,这又是可以优化的地方。
当前结果可由之前的结果得出,且有重复的搜索方法,就需要用DP。

public class Solution {
    public boolean canCross(int[] stones) {
        if(stones[1] != 1) return false;
        int n = stones.length;
        int[][] dp = new int[n][n];     // for ith stones, j means maximun previous steps
        for(int i=0; i<n;i++){
            for(int j=0; j<n;j++){
                dp[i][j] = -1;
            }
        }
        return canCross(dp, stones, 0, 0, n);
    }
    
    public boolean canCross(int[][] dp, int[] stones, int i, int j, int n){
        if(dp[i][j] != -1) return dp[i][j] == 1;
        if(i == n-1){
            dp[i][j] = 1; 
            return true;
        }
        
        for(int k=i+1; k < n; k++){
            if(stones[k] < stones[i] + j - 1){      // too close
                continue;
            } else if(stones[k] > stones[i] + j + 1){    // too far
                dp[i][j] = 0;
                continue;
            } else {   // j-1, j, j+1 three possibility 
                if(canCross(dp, stones, k, stones[k] - stones[i], n)){
                    dp[i][j] = 1;
                    return true;
                }
            }
        }
        
        dp[i][j] = 0;
        return false;
    }
}
public class Solution {
    public boolean canCross(int[] stones) {
        if(stones == null || stones.length == 0) return false;
        int n = stones.length;
        if(n == 1)  return true;
        if(stones[1] != 1) return false;
        if(n == 2)  return true;
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0; i < n; i++){
            if(i > 3 && stones[i] > stones[i-1]*2) return false;
            set.add(stones[i]);
        }
        
        return canCross(set, stones[n-1], 1, 1);
    }
    
    public boolean canCross(HashSet<Integer> set, int last, int pos, int jump){
        if(pos + jump == last || pos + jump + 1 == last || pos + jump -1 == last){
            return true;
        }
        if(set.contains(pos + jump + 1)){
            if(canCross(set, last, pos+jump+1, jump+1)) return true;
        }
        if(set.contains(pos + jump)){
            if(canCross(set, last, pos+jump, jump)) return true;
        }
        if(jump > 1 && set.contains(pos + jump-1)){
            if(canCross(set, last, pos+jump-1, jump-1)) return true;
        }
        return false;
    }
}

http://www.niftyadmin.cn/n/2524409.html

相关文章

web应用设计之我见

web应用设计这个话题看似有点搞笑&#xff0c;很多人可能会说&#xff0c;web应用有什么好设计的&#xff0c;ssh不是已经有了很好的分层架构模式了么&#xff1f;还需要什么设计&#xff1f;web应用不外乎就是请求处理和响应&#xff0c;诚然&#xff0c;简单的web应用的确不需…

使用 iMacros 来自动化日常的工作

利用 iMacros 的浏览器附加组件来提高工作效率 介绍 iMacros 这个强大的工具&#xff0c;使用简单的范例演示了如何使用这个工具来完成对于网页的操作&#xff0c;对于大量的具有重复性的工作内容尤其可以提高效率。对于测试人员或开发人员&#xff0c;这个工具也可以帮助管理 …

springboot 整合redis配置文件

redis配置 &#xff0c;properties文件 spring.redis.hostName: 192.168.174.128 spring.redis.port: 6379 spring.redis.password: xuan123456 spring.redis.database: 2 #默认使用db0 spring.redis.timeout: 0 spring.redis.pool.max-active: 8 spring.redis.pool.max…

Intelli IDEA 使用教程

1、怎样修改字体 File-Settings-color&Fonts-Fonts 转载于:https://www.cnblogs.com/lihubadboy/p/6118385.html

低调做人

一直以来&#xff0c;以为狂傲是年轻人应该有的&#xff0c;是不可避免的&#xff0c;而我也是这么做的&#xff0c;很少去特地的约束自己&#xff0c;有时候也挺无奈&#xff0c;说出的话&#xff0c;转身就开始后悔&#xff0c;"我怎么能这么说"&#xff0c;"…

爬取天猫国际、京东全球购、淘宝全球购的商品数据

公司内部mini项目–智慧选品 “智慧选品”项目主要是方便采购人员了解其他竞品平台的商品数据&#xff0c;将其他平台上卖的特别好的商品数据展示给采购人员&#xff0c;方便他们去采购商品&#xff0c;扩大公司自己的商品&#xff0c;所以就需要爬取其他平台的数据&#xff0…

nginx 之 grok 过滤

简介 前面我们的nginx日志编码使用的json&#xff0c;logstash直接输入预定义好的 JSON 数据&#xff0c;这样就可以省略掉 filter/grok 配置,但是在我们的生产环境中&#xff0c;日志格式往往使用的是普通的格式&#xff0c;因此就不得不使用logstash的filter/grok进行过滤&am…

个人备忘录

书籍 1、统计学习方法 清华大学出版社 26元 2、机器学习 从公理到算法 65元 3、周志华老师的 机器学习西瓜书 68元