<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)322:零錢兌換

          共 2656字,需瀏覽 6分鐘

           ·

          2021-07-18 11:20

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 零錢兌換,我們先來看題面:
          https://leetcode-cn.com/problems/coin-change/

          You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

          Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

          You may assume that you have an infinite number of each kind of coin.


          給定不同面額的硬幣 coins 和一個總金額 amount。編寫一個函數(shù)來計(jì)算可以湊成總金額所需的最少的硬幣個數(shù)。如果沒有任何一種硬幣組合能組成總金額,返回 -1。你可以認(rèn)為每種硬幣的數(shù)量是無限的。


          示例


          示例 1:

          輸入:coins = [1, 2, 5], amount = 11
          輸出:3
          解釋:11 = 5 + 5 + 1

          示例 2:

          輸入:coins = [2], amount = 3
          輸出:-1

          示例 3:

          輸入:coins = [1], amount = 0
          輸出:0

          示例 4:

          輸入:coins = [1], amount = 1
          輸出:1

          示例 5:

          輸入:coins = [1], amount = 2
          輸出:2


          解題

          https://blog.csdn.net/qq_23128065/article/details/104729144

          背包問題的思路,首先一個ans數(shù)組,存儲當(dāng)前index的錢應(yīng)該用幾個coins,主要思路就是,要么是當(dāng)前index中存儲的需要的硬幣個數(shù),要么是當(dāng)前index值的amount需要當(dāng)前ans[index]中存儲的硬幣個數(shù),要么是ans[index-coins[i]]+1,這個是如果加上當(dāng)前面值coins[i]的硬幣時(shí),那么當(dāng)前amount就是index-coins[i],所以查看ans[index-coins[i]]中存儲的硬幣個數(shù),然后加上當(dāng)前這一枚硬幣,就是amount為index時(shí)所需的硬幣個數(shù),取這兩種可能中的最小值就是所需最少硬幣個數(shù)。

          遞推方程如下:
          ans[i] = Math.min(ans[i],ans[i-coins[j]]+1);
          計(jì)算過程如下圖所示:
          最后的ans[amount]中存儲的就是兌換amount所需的硬幣個數(shù)。

          代碼如下:

          class Solution {
              public int coinChange(int[] coins, int amount) {
                  int[] ans = new int[amount+1];
                  Arrays.fill(ans,amount+1);
                  ans[0] = 0;
                  for(int j = 0;j<coins.length;j++){
                      for(int i=1;i<ans.length;i++){
                          if(i>=coins[j]){
                              ans[i] = Math.min(ans[i],ans[i-coins[j]]+1);
                          }
                      }
                  }
                  return ans[amount]>amount?-1:ans[amount];
              }
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:
          LeetCode1-320題匯總,希望對你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)321:拼接最大數(shù)

          瀏覽 41
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本丶欧美丶国产综合 | 99久热精品视频 | 久久亚洲热 | 欧美精品乱码久久久久蜜桃 | 伊人成人中文字 |