基础动态规划题

//Dynamic Programming test
//No1 Lintcode 669 coin changes
/*Description
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
Input example:
Input: 
[1, 2, 5]
11
Output: 3
Explanation: 11 = 5 + 5 + 1
QLink:  https://www.lintcode.com/problem/coin-change/description
O(m*n) m = target coins Max,n = length of coins
dp[Max+1]
Initialization:dp[0] = 0
Status law:dp[i] = max(dp[i-coins[0]]+1...dp[i-coins[coins.lenght-1]]+1,dp[i])
*/
//AC code 
public class Solution {
    /**
     * @param coins: a list of integer
     * @param amount: a total amount of money amount
     * @return: the fewest number of coins that you need to make up
     */
    public int coinChange(int[] coins, int Max) {
    	//dP数组
        int[] dp = new int[Max+1];
        //无穷大
        int MAXVALUE = 0x3f3f3f3f;
        //初始化
        dp[0] = 0;
        //求dP[1]~dp[Max]
        for(int i = 1;i <= Max;i++){
        	dp[i] = MAXVALUE;
        	for(int j = 0; j < coins.length;j++){
        		if(i>=coins[j] && dp[i-coins[j]]!=MAXVALUE){
        			dp[i] = Math.min(dp[i-coins[j]]+1, dp[i]);
        		}
        	}
        }
        if(dp[Max]==MAXVALUE){
        	dp[Max] = -1;
        }
        return dp[Max];
    }
}
//No2 Lintcode 114 Unique Paths
/*
Description
A robot is located at the top-left corner of a m x n grid.
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid.
How many possible unique paths are there?
Example 1:
Input: n = 1, m = 3
Output: 1	
Explanation: Only one path to target position.
*/
//dp[m][n]
//Initialization:if i = 0 or j = 0,dp[i][j] = 1
//status law:dp[i][j] = dp[i-1][j] + dp[i][j-1]
//O(m*n)
//Qlink:https://www.lintcode.com/problem/unique-paths/
//AC code
public class Solution {
    /**
     * @param m: positive integer (1 <= m <= 100)
     * @param n: positive integer (1 <= n <= 100)
     * @return: An integer
     */
        int uniquePaths(int m, int n) {
        //状态数组
        int[][] dp = new int[m][n];
        dp[0][0] = 1;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(i==0 || j==0){
                    dp[i][j] = 1;
                }
            }
        }
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(i>=1 && j>=1){
                    dp[i][j] = dp[i-1][j]+dp[i][j-1];
                }
            }
        }
        return dp[m-1][n-1];
    }
}
//No3 Lintcode 116 Jump Game
/*
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
Example 1
Input : [2,3,1,1,4]
Output : true
*/
//dp[length]
//Initialization:dp[0] = true
//dp[i] = OR(0<=j<i){dp[j]&& A[j]+j>=i},if have one true,dp[i] is true,else dp[i] = false
//O(n^2),if use greedy algorithm,O(n)
//QLink:https://www.lintcode.com/problem/jump-game/my-submissions
//AC code:
public class Solution {
    /**
     * @param A: A list of integers
     * @return: A boolean
     */
    public boolean canJump(int[] A) {
        int n = A.length;
        boolean[] dp =  new boolean[n];
        dp[0] = true;
        for(int i = 1;i < n;i++){
            dp[i] = false;
            for(int j = 0; j < i;j++){
        //when j in[0,i),if j + A[j] < i,then it can't jump to i
                if(dp[j] && j + A[j]>=i){
                    dp[i] = true;
                }
            }
        }
        return dp[n-1];
    }
}
//No4 Lintcode 191 Maximum Product Subarray
/*
Description
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input:[2,3,-2,4]
Output:6
Example 2:
Input:[-1,2,4,1]
Output:8
Note:you need to analyse nums[i] is greater than 0 or less than 0,then to Maintain a max dp array dp[length] and a min dp array dpm[length]
*/
/*
dp[0] = dpm[0] = nums[0]
status law:
nums[i]>0 then dp[i] = Math.max(dp[i-1]*nums[i],nums[i]);
               dpm[i] = Math.min(dpm[i-1]*nums[i],nums[i])
nums[i]<0 then dp[i] = Math.max(dpm[i-1]*nums[i],nums[i]);
               dpm[i] = Math.min(dp[i-1]*nums[i],nums[i])
QLink:https://www.lintcode.com/problem/maximum-product-subarray/my-submissions
*/
//AC code:
public class Solution {
    /**
     * @param nums: An array of integers
     * @return: An integer
     */
    public int maxProduct(int[] nums) {
        int n = nums.length;
        //max
        int[] dp  = new int[n];
        //min
        int[] dpm = new int[n];
        dp[0] = dpm[0] = nums[0];
        for(int i = 1;i < n;i++){
                dp[i] = dpm[i] = nums[i];
                if(nums[i]>0){
                    dp[i] = Math.max(dp[i-1]*nums[i],nums[i]);
                    dpm[i] = Math.min(dpm[i-1]*nums[i],nums[i]);
                }
                else if(nums[i]<0){
                    dp[i] = Math.max(dpm[i-1]*nums[i],nums[i]);
                    dpm[i] = Math.min(nums[i],dp[i-1]*nums[i]);
                }
        }
        int M = dp[0];
        for(int i = 0;i < n;i++){
            if(dp[i]>M){
                M = dp[i];
            }
        }
        return M;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w͏l͏j͏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值