[Algorithm]九章九之二:Sequence

116. Jump Game:点击打开链接

思路:贪心

例如:[2,3,1,1,4]--每个位置可以跳的步数

         0,1,2,3,4 --每个位置index

分析:初始化reach=nums[0]可以跳2步,从1开始遍历i,遍历到的位置+该位置可以走的步数>=当前的reach

         更新reach值就是可以到达的位置

         直到reach大于数组最后一个位置,说明最后一个位置可以到达

public class Solution {
    public boolean canJump(int[] nums) {
        if(nums==null || nums.length==0){                     //这边的corner case不是必要的
            return false;
        }
        
        int reach=nums[0];
        for(int i=1;i<nums.length;i++){
            if(reach>=i && nums[i]+i>=reach){                 //reach>=i才能使之前的reach包含新i的起步点
                reach=nums[i]+i;                              //更新reach
            }
        }
        return reach>=nums.length-1;                          //直到可以reach最后一个位置
    }
}

为了练习DP,LintCode降低测试数据量使可以accepted

思路:DP

例如:[2,3,1,1,4]--每个位置可以跳的步数

         0,1,2,3,4 --每个位置index

分析:从1开始遍历i,对于每个i只需要依次判断所有比i小的位置j能不能到i

         只要比i小的位置j可以到,并且从j开始可以以j能跳的步数A[j],一下跳到i或者跳过i,就表示可以跳到i

         break当前i的循环,直到遍历到数组最后一个位置,说明i从0到nums.length-1都可以到,就说明可以跳到数组最后一个位置

public class Solution {
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    public boolean canJump(int[] A) {
        boolean[] can=new boolean[A.length];
        can[0]=true;
        
        for(int i=1;i<A.length;i++){
            for(int j=0;j<i;j++){
                if(can[j] && A[j]+j>=i){
                    can[i]=true;
                    break;
                }
            }
        }
        return can[A.length-1];
    }
}

198. House Robber

class Solution {
    public int rob(int[] nums) {
        if(nums == null || nums.length == 0)
        {
            return 0;
        }
        
        if(nums.length == 1)
        {
            return nums[0];
        }
        
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1]= Math.max(nums[0], nums[1]);
        
        
        //每一个都是当前位置的值加上前两个位置的动态积累最大值, 
        //与当前位置的前一个的动态积累最大值的比较
        for(int i=2; i<nums.length; i++)
        {
            dp[i] = Math.max(nums[i] + dp[i-2], dp[i-1]);
        }
        
        return dp[nums.length - 1];
    }
}

非常易懂详细的讲解: http://www.cnblogs.com/grandyang/p/4383632.html

152. Maximum Product Subarray

class Solution {
    public int maxProduct(int[] nums) {
        if(nums == null || nums.length == 0)
        {
            return 0;
        }
        
        int result = nums[0];          //这边也是要注意的小地方
        
        int[] f = new int[nums.length];
        int[] g = new int[nums.length];
        f[0] = g[0] = nums[0];          //nums[0]是要初始化的值
        
        for(int i=1; i<nums.length; i++)
        {
            //每一个f[i]都是当前nums[i]时动态积累的最大值
            //最大值可能是三者之一: 
            //当前值nums[i]与动态积累当前值之前所有积累的最大值f[i-1]的乘积
            //当前值nums[i]与动态积累当前值之前所有积累的最小值g[i-1]的乘积
            //当前值nums[i]
            f[i] = Math.max(Math.max(f[i-1]*nums[i],g[i-1]*nums[i]), nums[i]);

            //每一个g[i]都是当前nums[i]时动态积累的最小值
            g[i] = Math.min(Math.min(f[i-1]*nums[i],g[i-1]*nums[i]), nums[i]);
            
            result = Math.max(f[i], result);
        }
        return result;       
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值