动态规划找零问题O(nk)

主要思路是,用一个数组coinsUsed[]来保存找i分钱所需的硬币数,(i==maxchange就是我们正在寻找的解),用一个数组lastCoin[]来保存哪一个硬币是最后用来得到最佳找零方案的信息。coins[] 用来记录硬币零钱有哪几种,differentCoins记录下coins[]的长度。maxChange记录最后的要兑换的零钱数。该算法复杂度为O(NK),N为不同面值的硬币数目,K是我们要找的的零钱数量。

 

 

 

Java代码    收藏代码
  1. public final class MakeChange  
  2. {  
  3.     // Dynamic programming algorithm to solve change making problem.  
  4.     // As a result, the coinsUsed array is filled with the  
  5.     // minimum number of coins needed for change from 0 -> maxChange  
  6.     // and lastCoin contains one of the coins needed to make the change.  
  7.     public static void makeChange( int [ ] coins, int differentCoins,  
  8.                 int maxChange, int [ ] coinsUsed, int [ ] lastCoin )  
  9.     {  
  10.         coinsUsed[ 0 ] = 0; lastCoin[ 0 ] = 1;  
  11.   
  12.         forint cents = 1; cents <= maxChange; cents++ )  
  13.         {  
  14.             int minCoins = cents;  
  15.             int newCoin  = 1;  
  16.   
  17.             forint j = 0; j < differentCoins; j++ )  
  18.             {  
  19.                 if( coins[ j ] > cents )   // Cannot use coin j  
  20.                     continue;  
  21.                 if( coinsUsed[ cents - coins[ j ] ] + 1 < minCoins )  
  22.                 {  
  23.                     minCoins = coinsUsed[ cents - coins[ j ] ] + 1;  
  24.                     newCoin  = coins[ j ];  
  25.                 }  
  26.             }  
  27.   
  28.             coinsUsed[ cents ] = minCoins;  
  29.             lastCoin[ cents ]  = newCoin;  
  30.         }  
  31.     }  
  32.   
  33.     // Simple test program  
  34.     public static void main( String [ ] args )  
  35.     {  
  36.         // The coins and the total amount of change  
  37.         int numCoins = 5;  
  38.         int [ ] coins = { 15102125 };  
  39.         int change = 0;  
  40.   
  41.         if( args.length == 0 )  
  42.         {  
  43.             System.out.println( "Supply a monetary amount on the command line" );  
  44.             System.exit( 0 );  
  45.         }  
  46.   
  47.         try  
  48.           { change = Integer.parseInt( args[ 0 ] ); }  
  49.         catch( NumberFormatException e )  
  50.         {  
  51.             System.out.println( e );  
  52.             System.exit( 0 );   
  53.         }  
  54.   
  55.         int [ ] used = new int[ change + 1 ];  
  56.         int [ ] last = new int[ change + 1 ];  
  57.   
  58.         makeChange( coins, numCoins, change, used, last );  
  59.   
  60.         System.out.println( "Best is " + used[ change ] + " coins" );  
  61.   
  62.         forint i = change; i > 0; )  
  63.         {  
  64.             System.out.print( last[ i ] + " " );  
  65.             i -= last[ i ];  
  66.         }  
  67.         System.out.println( );  
  68.     }  

详细解释可参见教材P89

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值