leetcode 518. 零錢兌換 II

image.png
給定不同面額的硬幣和一個總金額。寫出函數來計算可以湊成總金額的硬幣組合數。假設每一種面額的硬幣有無限個。

示例 1:

輸入: amount = 5, coins = [1, 2, 5]
輸出: 4
解釋: 有四種方式可以湊成總金額:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
示例 2:

輸入: amount = 3, coins = [2]
輸出: 0
解釋: 只用面額2的硬幣不能湊成總金額3。
示例 3:

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

注意:

你可以假設:

  • 0 <= amount (總金額) <= 5000
  • 1 <= coin (硬幣面額) <= 5000
  • 硬幣種類不超過 500 種
  • 結果符合 32 位符號整數

解題思路

數組定義

dp[i]代表金額為i時的組合數

狀態轉移

遍歷所有金額的情況,當前金額i,可能由金額i-coin的情況轉移而來

dp[i]+=dp[i-coin];

因為最外層循環遍歷了所有硬幣,所以可以排除了重復的組合數

代碼

class Solution {public int change(int amount, int[] coins) {int[] dp = new int[amount + 1];dp[0]=1;for (int coin : coins) {for (int i=coin;i<=amount;i++){dp[i]+=dp[i-coin];}}return dp[amount];}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390661.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390661.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390661.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

軟件測試中什么是正交實驗法_軟件工程中的正交性

軟件測試中什么是正交實驗法正交性 (Orthogonality) In software engineering, a system is considered orthogonal if changing one of its components changes the state of that component only. 在軟件工程中&#xff0c;如果更改系統的組件之一僅更改該組件的狀態&#xf…

leetcode 279. 完全平方數(dp)

題目一 給定正整數 n&#xff0c;找到若干個完全平方數&#xff08;比如 1, 4, 9, 16, …&#xff09;使得它們的和等于 n。你需要讓組成和的完全平方數的個數最少。 給你一個整數 n &#xff0c;返回和為 n 的完全平方數的 最少數量 。 完全平方數 是一個整數&#xff0c;其…

github代碼_GitHub啟動代碼空間

github代碼Codespaces works like a virtual Integrated Development Environment (IDE) on the cloud.代碼空間的工作方式類似于云上的虛擬集成開發環境(IDE)。 Until now, you had to make a pull request to contribute to a project. This required setting up the enviro…

php變量

什么叫變量&#xff1f; 變量可以通過變量名訪問。在指令式語言中&#xff0c;變量通常是可變的&#xff1b; 這里就先這么簡單理解&#xff0c;通過對語言的研究會更加的理解變量的其他意義。 在PHP中變量是用于存儲信息的"容器"&#xff1a; <?php $x5; $y6;…

js將base64做UrlEncode轉碼

使用 encodeURIComponent() 其詳細介紹 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent var base64 &#xff08;base64值&#xff09;encodeURIComponent(base64 ) //轉化 轉載于:https://www.cnblogs.com/xll-qg/p…

引用自己創建的css樣式表_如何使用CSS創建聯系表

引用自己創建的css樣式表First we create the HTML elements - input fields for First Name, Last Name, Email and a Text Area for the message.首先&#xff0c;我們創建HTML元素-名字&#xff0c;姓氏&#xff0c;電子郵件和消息的文本區域的輸入字段。 Later we apply C…

leetcode 1449. 數位成本和為目標值的最大數字(dp)

這是我參與更文挑戰的第12天 &#xff0c;活動詳情查看更文挑戰 題目 給你一個整數數組 cost 和一個整數 target 。請你返回滿足如下規則可以得到的 最大 整數&#xff1a; 給當前結果添加一個數位&#xff08;i 1&#xff09;的成本為 cost[i] &#xff08;cost 數組下標…

風能matlab仿真_風能產量預測—深度學習項目

風能matlab仿真DL DATATHON- AI4ImpactDL DATATHON- AI4影響 Published by Team AI Traders — Suyash Lohia, Nguyen Khoi Phan, Nikunj Taneja, Naman Agarwal and Mihir GuptaAI交易員團隊發布 -Suyash Lohia&#xff0c;Nguyen Khoi Phan&#xff0c;Nikonj Taneja&#x…

android JNI調用(Android Studio 3.0.1)(轉)

最近回頭復習了一下android 的jni調用&#xff0c;卻發現按以前的方法調用失敗&#xff0c;一怒之下就重新摸索&#xff0c;碰了幾次壁&#xff0c;發現網上好多教程都不能成功調用&#xff0c;于是記錄一下現在AS版本成功好用的調用方法。 這里設定你的ndk已經下載并且設置沒問…

安卓源碼 代號,標簽和內部版本號

SetupSecurityPortingTuningCompatibilityReference轉到源代碼Getting Started OverviewCodelines, Branches, and ReleasesCodenames, Tags, and Build NumbersProject RolesBrand GuidelinesLicensesFAQSite UpdatesDownloading and Building RequirementsEstablishing a Bui…

git 列出標簽_Git標簽介紹:如何在Git中列出,創建,刪除和顯示標簽

git 列出標簽Tagging lets developers mark important checkpoints in the course of their projects development. For instance, software release versions can be tagged. (Ex: v1.3.2) It essentially allows you to give a commit a special name(tag).通過標記&#xff…

leetcode 278. 第一個錯誤的版本(二分)

題目 你是產品經理&#xff0c;目前正在帶領一個團隊開發新的產品。不幸的是&#xff0c;你的產品的最新版本沒有通過質量檢測。由于每個版本都是基于之前的版本開發的&#xff0c;所以錯誤的版本之后的所有版本都是錯的。 假設你有 n 個版本 [1, 2, …, n]&#xff0c;你想找…

騰訊哈勃_用Python的黑客統計資料重新審視哈勃定律

騰訊哈勃Simple OLS Regression, Pairs Bootstrap Resampling, and Hypothesis Testing to observe the effect of Hubble’s Law in Python.通過簡單的OLS回歸&#xff0c;配對Bootstrap重采樣和假設檢驗來觀察哈勃定律在Python中的效果。 In this post, we will revisit Hub…

JAVA中動態編譯的簡單使用

一、引用庫 pom文件中申明如下&#xff1a; <dependencies><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><…

程序員實用小程序_我從閱讀《實用程序員》中學到了什么

程序員實用小程序In short: old but gold.簡而言之&#xff1a;古老而又黃金。 Published in 1999, The Pragmatic Programmer is a book about how to become a Pragmatic Programmer. Which really means a ‘Good Programmer’. 《實用程序員》于1999年出版&#xff0c;是一…

leetcode 5786. 可移除字符的最大數目(二分法)

題目 給你兩個字符串 s 和 p &#xff0c;其中 p 是 s 的一個 子序列 。同時&#xff0c;給你一個元素 互不相同 且下標 從 0 開始 計數的整數數組 removable &#xff0c;該數組是 s 中下標的一個子集&#xff08;s 的下標也 從 0 開始 計數&#xff09;。 請你找出一個整數…

如何使用Picterra的地理空間平臺分析衛星圖像

From April-May 2020, Sentinel-Hub had organized the third edition of their custom script competition. The competition was organized in collaboration with the Copernicus EU Earth Observation programme, the European Space Agency and AI4EO consortium.從2020年…

df -l查看本地文件系統

df -l, --locallimit listing to local file systems 轉載于:https://www.cnblogs.com/jonathanyue/p/9301222.html

在Packet Tracer中路由器靜態路由配置

實驗目標&#xff1a;<1>掌握靜態路由的配置方法和技巧<2>掌握通過靜態路由方式實現網絡的連通性<3>熟悉廣域網線纜的鏈接方式技術原理&#xff1a;<1>路由器屬于網絡層設備&#xff0c;能夠根據IP包頭的信息&#xff0c;選擇一條最佳路徑&#xff0c;…

python示例_帶有示例的Python功能指南

python示例Python函數簡介 (Introduction to Functions in Python) A function allows you to define a reusable block of code that can be executed many times within your program.函數允許您定義一個可重用的代碼塊&#xff0c;該代碼塊可以在程序中多次執行。 Function…