Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it's not possible to select coins in such a way that they sum up to S.
For a better understanding let's take this example:
Given coins with values 1, 3, and 5.
And the sum S is set to be 11.
First of all we mark that for state 0 (sum 0) we have found a solution with a minimum number of 0 coins. We then go to sum 1. First, we mark that we haven't yet found a solution for this one (a value of Infinity would be fine). Then we see that only coin 1 is less than or equal to the current sum. Analyzing it, we see that for sum 1-V1= 0 we have a solution with 0 coins. Because we add one coin to this solution, we'll have a solution with 1 coin for sum 1. It's the only solution yet found for this sum. We write (save) it. Then we proceed to the next state - sum 2. We again see that the only coin which is less or equal to this sum is the first coin, having a value of 1. The optimal solution found for sum (2-1) = 1 is coin 1. This coin 1 plus the first coin will sum up to 2, and thus make a sum of 2 with the help of only 2 coins. This is the best and only solution for sum 2. Now we proceed to sum 3. We now have 2 coins which are to be analyzed - first and second one, having values of 1 and 3. Let's see the first one. There exists a solution for sum 2 (3 - 1) and therefore we can construct from it a solution for sum 3 by adding the first coin to it. Because the best solution for sum 2 that we found has 2 coins, the new solution for sum 3 will have 3 coins. Now let's take the second coin with value equal to 3. The sum for which this coin needs to be added to make 3 , is 0. We know that sum 0 is made up of 0 coins. Thus we can make a sum of 3 with only one coin - 3. We see that it's better than the previous found solution for sum 3 , which was composed of 3 coins. We update it and mark it as having only 1 coin. The same we do for sum 4, and get a solution of 2 coins - 1+3. And so on.
Pseudocode:
Set Min[i] equal to Infinity for all of i Min[0]=0 For i = 1 to S For j = 0 to N - 1 If (Vj<=i AND Min[i-Vj]+1<Min[i]) Then Min[i]=Min[i-Vj]+1 Output Min[S]
Here are the solutions found for all sums:
Sum | Min. nr. of coins | Coin value added to a smaller sum to obtain this sum (it is displayed in brackets) |
0 | 0 | - |
1 | 1 | 1 (0) |
2 | 2 | 1 (1) |
3 | 1 | 3 (0) |
4 | 2 | 1 (3) |
5 | 1 | 5 (0) |
6 | 2 | 3 (3) |
7 | 3 | 1 (6) |
8 | 2 | 3 (5) |
9 | 3 | 1 (8) |
10 | 2 | 5 (5) |
11 | 3 | 1 (10) |
As a result we have found a solution of 3 coins which sum up to 11.
public int findMinCoins(int[] coins, int S) { int N = coins.length; int[] f = new int[S+1]; Arrays.fill(f, Integer.MAX_VALUE); f[0] = 0; for(int i=0; i<=S; i++) { for(int j=0; j<N; j++) { if(coins[j] <= i) { f[i] = Math.min(f[i-coins[j]] + 1, f[i]); } } } return f[S]; }
Reference:
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg
相关推荐
Coin3D是一个开源的三维图形库,主要用于构建交互式的3D应用程序。这个“coin3D-3.1.3-SoQt-SoWin-VC10.7z”压缩包包含的是Coin3D的3.1.3版本,适配于Qt和Windows平台,且是用Visual Studio 2010 (VC10) 编译的...
《Coin3D-Qt.PDF》是一份详细探讨Coin3D与Qt集成的文档,它主要阐述了如何在Qt应用程序中利用Coin3D库来创建3D图形界面。Coin3D是一个开源的3D图形库,它实现了OpenInventor API,而Qt则是一个广泛使用的C++图形用户...
《PyPI与CoinMetrics API客户端:深入理解与应用》 PyPI(Python Package Index)是Python程序员的宝库,它提供了无数的开源Python库供开发者使用。在标题中提到的"PyPI 官网下载 | coinmetrics-api-client-2021.11...
《Coin3D与Qt结合开发详解》 在IT行业中,跨平台的图形用户界面(GUI)开发是一项重要的任务,而Coin3D与Qt的结合应用则为此提供了强大的解决方案。本资料主要围绕“Coin3D-Qt.zip_Coin3D_Molongcoin-qt代理_qt-...
Coin3D是一个开源的3D图形库,主要版本为3.1.3,适用于Linux、Windows和Qt平台。这个库提供了强大的三维图形渲染能力,旨在支持Scene Graph API,类似于OpenGL的面向对象接口,使得开发者可以更高效地创建复杂的3D...
coin-or-Clp-devel-1.16.10-1.el7.x86_64.rpm
《Coin-3.1.3-SoWin-1.5.0-vs2008:在Visual Studio 2008中的3D图形编程实践》 Coin是一个强大的三维图形库,它是Open Inventor的开源实现,广泛应用于科学可视化、工程设计、虚拟现实等领域。这个名为“Coin-3.1.3...
在计算机科学领域,"Coin Change"问题是一个经典的问题,它涉及到计算在给定一组不同面值的硬币时,组成特定总金额的最少硬币数量。这个问题源自实际生活中的找零场景,但它的理论意义远超其应用场景。在本篇讨论中...
coin-or-Cbc-devel-2.9.8-1.el7.x86_64.rpm
首先,"Coin-3.0.0-bin-msvc6.zip"是一个包含Coin3D 3.0版本的库文件和相关资源的压缩包,特别针对VC6.0编译环境进行了优化。在解压后,我们可以看到四个主要的文件夹:data、bin、include和lib。 1. **data** ...
coin-or-Cbc-2.9.8-1.el7.x86_64.rpm
"Coin-slider-master" 是一个可能与网页开发相关的项目,标题和描述都反映了这一点。这个项目的重点可能是实现一种基于Coin Slider的交互式元素,这通常是一种动态展示图片或内容的滑动展示组件,常用于网站的轮播图...
《Open Inventor在Windows平台上的实现:Coin-3.1.3与SoWin-1.5.0在Visual Studio 2010下的编译详解》 Open Inventor是一款强大的三维图形开发库,广泛应用于科学可视化、CAD以及游戏开发等领域。在Windows操作系统...
coin-or-CoinUtils-doc-2.10.13-1.el7.noarch.rpm
c c语言_leetcode题解之0322_coin_change
coin-or-Clp-doc-1.16.10-1.el7.noarch.rpm
coin-or-Sample-1.2.10-5.el7.noarch.rpm
coin-or-Cbc-doc-2.9.8-1.el7.noarch.rpm
coin-or-Clp-1.16.10-1.el7.x86_64.rpm
coin-or-Cgl-doc-0.59.9-1.el7.noarch.rpm