leetCode动态规划(java):

题目转载自: https://leetcode-cn.com/problems/unique-paths/ 官方网站

322. Coin Change

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.

Example 1:

Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Note:
You may assume that you have an infinite number of each kind of coin.

class Solution {
    public int coinChange(int[] coins, int amount) {
        int length = coins.length;
        int []dp   = new int[amount + 1];
        dp[0] = 0; //初始化条件
        for(int i = 1; i <= amount; i++){
                dp[i] = Integer.MAX_VALUE;
                for(int j = 0; j < length; j++){
                    //确保无穷大不越界 ,确定硬币的边界条件, 保证硬币的值 始终 > 0(因为dp数组下标不能为0)  因此要先判断 i >= coins (保证下标不为负数)
                    if(i >= coins[j] && dp[i - coins[j]] != Integer.MAX_VALUE)  
                        dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]); //确定所选硬币数量最少,动态规划转移方程
                }
        }
        if(dp[amount] == Integer.MAX_VALUE)
            return -1;
        else
            return dp[amount];
    }
}

62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

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 (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 7 x 3 grid. How many possible unique paths are there?

动态方程:edge[i][j] = edge[i - 1][j] + edge[i][j - 1]; (因为机器人只能走下面 和 右边)

边界条件:最左边的行列 都置为1 因为只有一种走法

 

class Solution {
    public int uniquePaths(int m, int n) {
        int [][]edge = new int[m][n];
        for(int i = 0; i < m;i++){
            for(int j = 0; j < n; j++){
                if(i == 0 || j == 0){
                    edge[i][j] = 1;
                }else{
                    edge[i][j] = edge[i - 1][j] + edge[i][j - 1];
                }
            }
        }
        return edge[m - 1][n - 1];
    }
}

55. 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
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

动态规划实现 :(时间复杂度为 o(n2)):

class Solution {
    public boolean canJump(int[] nums) {
        boolean []dp = new boolean[nums.length];
        dp[0] = true;
        for(int i = 1; i < nums.length; i++){
            for(int j = 0; j < i; j++){
                if(dp[j] && j + nums[j] >= i){
                    dp[i] = true;
                    break;//判断一个点可以到达后 就跳出循环, 因为dp的时间复杂度为O(n2)
                }
            }
        }
        return dp[nums.length - 1];
    }
}

贪心算法实现:

 时间复杂度为 o(n):

1. 正向贪心:

                从 第一个石头开始出发, 遍历到 第n个石头

                设定一个最大跳跃距离, 每次跳格时, 判断最大距离, 并更新, 如果此时石头的距离已经超过了最大距离那么就

返回false。

class Solution {
    public boolean canJump(int[] nums) {
        int maxIndex = nums[0];
        for(int i = 0; i < nums.length; i++){
            if(i <= maxIndex ){
                if(nums[i] + i >= nums.length - 1)
                    return true;
                if(nums[i] + i > maxIndex){
                    maxIndex = nums[i] + i;
                }
            }else{
                return false;
            }
        }
        return true;
    }
}

2.逆向贪心:

                 设定 position为 从最后一块石头开始出发, 从 后往前遍历, 如果 前面的石头能够跳到此时的position

就更新此时的position, 如果最终的position的值为0(表示最终能到达终点), 那么就返回true.

class Solution {
    public boolean canJump(int[] nums) {
        int maxIndex = nums.length - 1;
        for(int i = nums.length - 1; i >= 0; i--){
            if(nums[i] + i >= maxIndex)
                maxIndex = i;
        }
        return maxIndex == 0;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值