【LeetCode】322. Coin Change 硬币找零

2024-01-15 21:18

本文主要是介绍【LeetCode】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.

Example1:
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.


本题含义:给定一些面值的硬币,假设每种面值的硬币数量无限,在给定一个钱数,问最少用几个硬币来找零,如果给定的硬币不能凑出给定的钱数,则返回-1。
这道题其实题后面给出了较为详细的解答,本文主要是为了加深印象,巩固基础方便复习,对其中的内容进行摘录。详情请见Leetcode 322. Coin Change. 网站中提出了三种解决方案。第一种是用Brute force,本文省略,主要记录下是用动态规划进行求解的方法。
这道题属于完全背包问题。

思路一: Dynamic programming - Top down

F ( a m o u n t ) F(amount) F(amount) 表示是用 c o i n s [ 0 ] , c o i n s [ 1 ] , ⋅ ⋅ ⋅ , c o i n s [ n − 1 ] coins[0], coins[1], \cdot\cdot\cdot,coins[n-1] coins[0],coins[1],,coins[n1]构成的最少数量
动态规划思想的核心是寻找最优子问题,因此本题的最主要的目标就是寻找该问题的最优子问题。
假设 F ( S ) F(S) F(S)表示由硬币数组 c o i n s coins coins构成需要的最少数量的硬币数,并且假设最后一个构成 S S S的硬币是 C C C,因此 F ( S ) = F ( S − C ) + 1 F(S) = F(S-C) + 1 F(S)=F(SC)+1但实际上我们不确定最后构成 S S S的硬币是 c o i n s = { c 0 , c 1 , ⋅ ⋅ ⋅ , c n − 1 } coins = \{c_0, c_1,\cdot\cdot\cdot,c_{n-1}\} coins={c0,c1,,cn1}中的哪一个,因此我们进行遍历计算 F ( S − c i ) F(S-c_i) F(Sci),选择其中 F ( S − c i ) F(S-c_i) F(Sci)最小的那个 c i c_i ci
F ( S ) = { 0 , if  S = 0 m i n i = 0 , ⋅ ⋅ ⋅ , n − 1 F ( S − c i ) + 1 , if  S − c i ≥ 0 − 1 other  F(S)= \begin{cases} 0, & \text {if $S=0$ } \\ min_{i=0,\cdot\cdot\cdot,n-1}F(S-c_i) + 1, & \text{if $S-c_i \geq 0$ }\\ -1 & \text{other } \end{cases} F(S)=0,mini=0,,n1F(Sci)+1,1if S=if Sciother 

自顶向下求解

算法
该算法主要思想是自顶下下的解决方式。主要使用回溯以及在回归树中进行减枝。为了防止重复计算,使用一个数组存储子问题的解决方案。以下是代码

public class Solution {public int coinChange_TopDown(int[] coins, int amount) {if (amount <= 0)return 0;return coinChange(coins, amount, new int[amount]);}/*** @param count: 存储子问题的解* */public int coinChange(int[] coins, int remain, int[] count) {if (remain < 0)return -1;if (remain == 0)return 0;if (count[remain - 1] != 0)return count[remain - 1];int min = Integer.MAX_VALUE;for (int coin: coins) {int res = coinChange(coins, remain - coin, count);if (res >= 0 && res < min)min = res + 1;}count[remain - 1] = (min == Integer.MAX_VALUE) ? -1 : min;return count[remain - 1];}
}

复杂度分析:
时间复杂度为: O ( S ∗ n ) O(S*n) O(Sn),其中 S S S是amount值本身, n n n是数组 c o i n s coins coins的大小
空间复杂度为: O ( S ) O(S) O(S),主要用于记录子问题的解


思路二: Dynamic programming - Bottom up

第二种主要是使用迭代的方法进行计算,在计算 F ( a m o u n t ) F(amount) F(amount)之前,已经计算好了构成 0 , 1 , ⋅ ⋅ ⋅ , n − 1 0,1, \cdot\cdot\cdot,n-1 0,1,,n1的解。因此计算 F ( a m o u n t ) = m i n i = 0 , ⋅ ⋅ ⋅ , n − 1 F ( a m o u n t − c o i n s [ i ] ) + 1 F(amount) = min_{i = 0,\cdot\cdot\cdot,n-1}F(amount-coins[i]) + 1 F(amount)=mini=0,,n1F(amountcoins[i])+1

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

复杂度分析:
时间复杂度为: O ( S ∗ n ) O(S*n) O(Sn),其中 S S S是amount值本身, n n n是数组 c o i n s coins coins的大小
空间复杂度为: O ( S ) O(S) O(S),主要用于记录子问题的解

这篇关于【LeetCode】322. Coin Change 硬币找零的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610233

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

XTU 1233 n个硬币连续m个正面个数(dp)

题面: Coins Problem Description: Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing head face or tail face o

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

fzu 2277 Change 线段树

Problem 2277 Change Time Limit: 2000 mSec    Memory Limit : 262144 KB  Problem Description There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划