Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                function coinChange(coins, amount) {
  let len = coins.length,
    min = Number.MAX_VALUE,
    dp = [new Array(amount)];
  for (let j = 1; j <= amount; j++) {	//初始化第一行
    //纸币面值比金额大,或金额无法整除纸币
	if(coins[0] > j || (j % coins[0]) > 0) {
      //对于无法支付金额的情况,赋值为0
      dp[0][j] = 0;
	  continue;
	}
    dp[0][j] = j / coins[0];			//得到纸币数量
  }
  for (let i = 1; i < len; i++) {
    dp[i] = [];
    for (let j = 1; j <= amount; j++) {
      let rest = j - coins[i];
	  if(rest == 0) {		//可用当前纸币支付金额
        dp[i][j] = 1;
		continue;
	  }
	  if(rest < 0) {		//当前纸币比支付金额大
        dp[i][j] = dp[i-1][j];
		continue;
	  }
	  if(dp[i-1][j] > 0 && dp[i][rest] > 0) {	//都可以支付金额
        dp[i][j] = Math.min(dp[i-1][j], dp[i][rest] + 1);
	  }else if(dp[i-1][j] > 0) {				//只有上一行可以支付金额
        dp[i][j] = dp[i-1][j];
	  }else if(dp[i][rest] > 0) {				//只能借助剩余金额的纸币数支付
        dp[i][j] = dp[i][rest] + 1;
	  }else {									//无法支付
        dp[i][j] = 0;
	  }
    }
  }
  //取金额的最小值
  for(let i = 0; i < len; i++) {
    if(dp[i][amount] == 0) {		//过滤掉无法支付的数据
	  continue;
	}
    if(dp[i][amount] > 0) {
	  min = Math.min(min, dp[i][amount]);
	}
  }
  return min == Number.MAX_VALUE ? -1 : min;
}

let coins = [1, 2, 5],
  amount = 11;
console.log("[1, 2, 5],11", coinChange(coins, amount));

coins = [2];
amount = 3;
console.log("[2],3", coinChange(coins, amount));

coins = [2,5,10,1];
amount = 27;
console.log("[2,5,10,1],27", coinChange(coins, amount));
              
            
!
999px

Console