Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 MinCoinChange(coins) {
      var coins = coins; //{1}该参数代表问题中的面额
      var cache = {}; //{2}为了更加高效且不重复计算值
      this.makeChange = function (amount) {
        var me = this;
        if (!amount) { //{3}若amount不为正(< 0),就返回空数组
          return [];
        }
        if (cache[amount]) { //{4}若结果已缓存,则直接返回结果;否则,执行算法。
          return cache[amount]; 
        }
        var min = [], newMin, newAmount;
        for (var i = 0; i < coins.length; i++) { //{5}
          var coin = coins[i];
          newAmount = amount - coin; //{6},对每个面额,我们都计算newAmount的值,它的值会一直减小,直到能找零的最小钱数
          if (newAmount >= 0) {
            newMin = me.makeChange(newAmount); //{7}
          }
          if (
            newAmount >= 0 && //{8}
            (newMin.length < min.length - 1 || !min.length)//{9}
            && (newMin.length || !newAmount)) //{10} 判断newAmount是否有效,minValue (最少硬币数)是否是最优解,与此同时minValue和newAmount是否是合理的值
          {
            min = [coin].concat(newMin); //{11}有一个比之前更优的答案
            console.log('new Min ' + min + ' for ' + amount);
          }
        }
        console.log(amount, min, 'cache')
        return (cache[amount] = min); //{12}返回最终结果
      };
    }

var minCoinChange = new MinCoinChange([1, 5, 10, 25]);
console.log(minCoinChange.makeChange(6));
              
            
!
999px

Console