`
阅读更多

Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter.

For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.

 

We are trying to count the number of distinct sets.

Since order does not matter, we will impose that our solutions (sets) are all sorted in non-decreasing order (Thus, we are looking at sorted-set solutions: collections).

For a particular N and S=\{S_{1},S_{2},\ldots ,S_{m}\} (now with the restriction that S_{1}<S_{2}<\ldots <S_{m}, our solutions can be constructed in non-decreasing order), the set of solutions for this problem, C(N,m), can be partitioned into two sets:

  • There are those sets that do not contain any S_{m} and
  • Those sets that contain at least 1 S_{m}

If a solution does not contain S_{m}, then we can solve the subproblem of N with S=\{S_{1},S_{2},\ldots ,S_{{m-1}}\}, or the solutions of C(N,m-1).

If a solution does contain S_{m}, then we are using at least one S_{m}, thus we are now solving the subproblem of N-S_{m}S=\{S_{1},S_{2},\ldots ,S_{m}\}. This is C(N-S_{m},m).

Thus, we can formulate the following:

C(N,m)=C(N,m-1)+C(N-S_{m},m)

with the base cases:

  • C(N,m)=1,N=0 (one solution -- we have no money, exactly one way to solve the problem - by choosing no coin change, or, more precisely, to choose coin change of 0)
  • C(N,m)=0,N<0 (no solution -- negative sum of money)
  • C(N,m)=0,N\geq 1,m\leq 0 (no solution -- we have money, but no change available)

先上最优解:

public int coinChange(int[] S, int N) {
	int[] f = new int[N+1];
	f[0] = 1;
	for(int i=0; i<S.length; i++) {
		for(int j=S[i]; j<=N; j++) {
			f[j] += f[j-S[i]];
		}
	}
	return f[N];
}

 

Recursive Method:

// Returns the count of ways we can sum  S[0...m-1] coins to get sum n
int count( int S[], int m, int n ) {
    // If n is 0 then there is 1 solution (do not include any coin)
    if (n == 0)
        return 1;
     
    // If n is less than 0 then no solution exists
    if (n < 0)
        return 0;
 
    // If there are no coins and n is greater than 0, then no solution exist
    if (m <=0 && n >= 1)
        return 0;
 
    // count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
    return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}

 

Dynamic Programming: 

int count( int S[], int m, int n )
{
    int i, j, x, y;
 
    // We need n+1 rows as the table is consturcted in bottom up manner using 
    // the base case 0 value case (n = 0)
    int table[n+1][m];
    
    // Fill the enteries for 0 value case (n = 0)
    for (i=0; i<m; i++)
        table[0][i] = 1;
 
    // Fill rest of the table enteries in bottom up manner  
    for (i = 1; i < n+1; i++)
    {
        for (j = 0; j < m; j++)
        {
            // Count of solutions including S[j]
            x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
 
            // Count of solutions excluding S[j]
            y = (j >= 1)? table[i][j-1]: 0;
 
            // total count
            table[i][j] = x + y;
        }
    }
    return table[n][m-1];
}

 

Time Complexity: O(mn)

Following is a simplified version of method 2. The auxiliary space required here is O(n) only.

int count( int S[], int m, int n )
{
    // table[i] will be storing the number of solutions for
    // value i. We need n+1 rows as the table is consturcted
    // in bottom up manner using the base case (n = 0)
    int table[n+1];
 
    // Initialize all table values as 0
    memset(table, 0, sizeof(table));
 
    // Base case (If given value is 0)
    table[0] = 1;
 
    // Pick all coins one by one and update the table[] values
    // after the index greater than or equal to the value of the
    // picked coin
    for(int i=0; i<m; i++)
        for(int j=S[i]; j<=n; j++)
            table[j] += table[j-S[i]];
 
    return table[n];
}

Reference: 

http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/

http://www.algorithmist.com/index.php/Coin_Change

分享到:
评论

相关推荐

    飞歌G6IV刷机包,恢复出厂解决车机问题

    飞歌G6IV刷机包,恢复出厂解决车机问题

    人工智能大作业-无人机图像目标检测.zip

    无人机最强算法源码,易于部署和学习交流使用

    node-v10.9.0-linux-s390x.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    Telecord机器人,无electron依赖。.zip

    无人机最强源码,无人机算法,易于部署和学习交流使用

    中国统计NJ面板数据-(更新至2022年)林业有害生物防治情况.xls

    数据来源:中国统计NJ-2023版

    无人机共享平台小程序.zip

    无人机最强算法源码,易于部署和学习交流使用

    node-v10.14.2-linux-ppc64le.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    en-PP-OCRv3-det.onnx

    PP-OCR det

    毫无特色的 QQ 机器人.zip

    无人机最强算法源码,易于部署和学习交流使用

    麦肯锡 营销 概述与基本框架gl.ppt

    麦肯锡 营销 概述与基本框架gl.ppt

    node-v9.6.0-linux-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    大四多无人机协同控制技术的MATLAB工程.zip

    无人机最强源码,无人机算法,易于部署和学习交流使用

    大疆无人机RTK自定义网络连接和移动站,亲测可用.zip

    无人机最强源码,无人机算法,易于部署和学习交流使用

    node-v9.7.1-linux-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    Kendryte K210人工智能芯片应用程序集合,包括人脸检测、颜色检测、目标检测

    Kendryte K210人工智能芯片应用程序集合,包括人脸检测、颜色检测、目标检测和分类、二维码和Apriltag代码检测以及和ArduPilot飞控软件的通信。这些应用程序已部署到无人机终端。This repository is a collection of appl….

    node-v11.9.0-linux-armv6l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    PHP开源 trx自动兑换机器人源码,一款自动USDT兑换TRX的telegram机器人源代码,完美无措,简单部署,直接运营

    PHP开源 trx自动兑换机器人源码,一款自动USDT兑换TRX的telegram机器人源代码,完美无措,简单部署,直接运营,无后门,无BUG,功能强大,带管理后台.zip

    基于51单片机+lcd12864显示俄罗斯方块小游戏MCU软件源代码.zip

    基于51单片机+lcd12864显示俄罗斯方块小游戏MCU软件源代码 * 单 片 机:STC89C52RC * 简 述:使用LCD12864显示的俄罗斯方块程序 * 功 能:计分,下一个方块预览,欢迎结束界面,长按连续左右移,暂停(按键Left+Turn) * IO口设定:按键 sbit key_sr_left=P3^7; sbit key_sr_turn=P3^6; sbit key_sr_right=P3^5; sbit key_sr_down=P3^4; LCD12864 sbit RS_Port=P1^0; sbit RW_Port=P1^1; sbit E_Port=P2^5; sbit PSB_Port=P1^2; sbit RST_Port=P1^4; 数码管锁存器 sbit dula=P2^6; sbit wela=P2^7;

    企业数智化转型全攻略:新质生产力赋能数字化运营体系建设方案.pptx

    企业数智化转型全攻略:新质生产力赋能数字化运营体系建设方案.pptx

    中国统计NJ面板数据-(更新至2022年)三次产业和主要行业贡献率.xls

    数据来源:中国统计NJ-2023版

Global site tag (gtag.js) - Google Analytics