LeetCode:零钱兑换【322】【DP】

LeetCode:零钱兑换【322】【DP】

题目描述

给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1

示例 1:

输入: coins = [1, 2, 5], amount = 11
输出: 3
解释: 11 = 5 + 5 + 1

示例 2:

输入: coins = [2], amount = 3
输出: -1

说明:
你可以认为每种硬币的数量是无限的。

题目分析

  很显然,这是一道动态规划问题。和第279完全平方数的解法如出一辙。

  我们求11的硬币数,相当于求n=10,n=9,n=6的银币数中的最小值+1.

  道理明白以后,这里我们倒着填并不是明智的。因为n=10,n=9,n=6的数据都是空的,同样前面的数也都是空的。

  所以我们采取自下而上的填充测略,可以减省空间的消耗

  

Java题解

class Solution {
    public int coinChange(int[] coins, int amount) {
        if(amount==0||coins.length<=0)
            return 0;
        int[] dp = new int[amount+1];
        Arrays.fill(dp,amount+1);
        Arrays.sort(coins);
        for(int i=1;i<=amount;i++)
        {
            for(int j=0;j<coins.length;j++)
            {
                if(i-coins[j]==0)
                    dp[i]=1;
                else if(i-coins[j]>0){
                    dp[i]=Math.min(dp[i],dp[i-coins[j]]+1);
                }
            }
        }
        return dp[amount] > amount ? -1 : dp[amount];
    }
}

原文地址:https://www.cnblogs.com/MrSaver/p/9669668.html

时间: 2024-11-08 22:41:10

LeetCode:零钱兑换【322】【DP】的相关文章

[LeetCode]322. 零钱兑换(DP)

题目 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回?-1. 示例?1: 输入: coins = [1, 2, 5], amount = 11 输出: 3 解释: 11 = 5 + 5 + 1 示例 2: 输入: coins = [2], amount = 3 输出: -1 说明: 你可以认为每种硬币的数量是无限的. 来源:力扣(LeetCode) 链接:https://leetcode

LeetCode——零钱兑换

Q:给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回?-1. 示例?1: 输入: coins = [1, 2, 5], amount = 11 输出: 3 解释: 11 = 5 + 5 + 1 示例 2: 输入: coins = [2], amount = 3 输出: -1 说明: 你可以认为每种硬币的数量是无限的. A: 第一反应是回溯我也是没谁了--然后内存就限制了我. public

leetcode 322 零钱兑换

目录 1. 题目 2. 解法 2.1. 暴力穷举 2.2. 动态规划-1 自下而上 2.3. 动态规划-2 自上而下 3. 代码及测试结果 3.1. 测试代码: 3.2. 结果: 1. 题目 零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = [1, 2, 5], amount = 11 输出: 3 解释: 11 = 5 + 5 + 1

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

[LeetCode] Triangle(&#39;Bottom-up&#39; DP)

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

SICP 习题 (2.19) 解题总结:重写零钱兑换程序

SICP 习题2.19 要求我们重新设计1.2.2节的零钱兑换程序,要求我们可以轻易改变程序里用的兑换币种. 我们先看看1.2.2节的零钱兑换程序,代码是这样的: (define (RMB-Change amount) (format #t "Changing ~S~%" amount) (cond ((= amount 0) 0) ((< amount 0) 0) (else (RMB-Change-Recursive amount 1 '() )))) (define (RM

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

[LeetCode] Unique Paths II(DP)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl