首页 > 其他分享 >LeetCode: 322. Coin Change

LeetCode: 322. Coin Change

时间:2022-12-05 18:06:51浏览次数:60  
标签:return int coins rhv 322 amount Coin LeetCode dp


LeetCode: 322. Coin Change

题目描述

You are given coins of different denominations and a total amount of money amount. Write a function to compute 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.

Example 1:

Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

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

解题思路 —— 动态规划

  1. 记:​​dp[i]​​ 为 钱数为 i 需要的最少硬币数 (-1 表示不可达)
  2. 状态转移方程:​​dp[i] = min{dp[i-coins[j]]+1}​​​,其中 ​​j = 0...len(coins)​

AC 代码

func min(lhv, rhv int) int {
switch {
case lhv == -1: return rhv
case rhv == -1: return lhv
case lhv < rhv: return lhv
default: return rhv
}
}

func coinChange(coins []int, amount int) int {
//dp[i]: amount 的钱需要的最少硬币数 (-1 表示不可达)
dp := []int {0}

for i := 1; i <= amount; i++ {
dp = append(dp, -1)

for j := 0; j < len(coins); j++ {
if i >= coins[j] && dp[i-coins[j]] != -1 {
dp[i] = min(dp[i], dp[i-coins[j]]+1)
}
}
}

return dp[amount]
}


标签:return,int,coins,rhv,322,amount,Coin,LeetCode,dp
From: https://blog.51cto.com/u_15903085/5913196

相关文章

  • LeetCode:295. Find Median from Data Stream
    LeetCode:295.FindMedianfromDataStream题目描述Medianisthemiddlevalueinanorderedintegerlist.Ifthesizeofthelistiseven,thereisnomiddleval......
  • LeetCode:282. Expression Add Operators
    LeetCode:282.ExpressionAddOperators题目描述Givenastringthatcontainsonlydigits0-9andatargetvalue,returnallpossibilitiestoaddbinaryoperators......
  • LeetCode:224.Basic Calculator
    LeetCode:224.BasicCalculator题目描述Implementabasiccalculatortoevaluateasimpleexpressionstring.Theexpressionstringmaycontainopen​​(​​​and......
  • LeetCode: 221. Maximal Square
    LeetCode:221.MaximalSquare题目描述Givena2Dbinarymatrixfilledwith​​0​​​'sand​​1​​​'s,findthelargestsquarecontainingonly​​1​​'s......
  • LeetCode: 223. Rectangle Area
    LeetCode:223.RectangleArea题目描述Findthetotalareacoveredbytworectilinearrectanglesina2Dplane.Eachrectangleisdefinedbyitsbottomleftcorne......
  • LeetCode: 225. Implement Stack using Queues
    LeetCode:225.ImplementStackusingQueues题目描述Implementthefollowingoperationsofastackusingqueues.​​push(x)​​–Pushelementxontostack.​......
  • LeetCode: 232. Implement Queue using Stacks
    LeetCode:232.ImplementQueueusingStacks题目描述Implementthefollowingoperationsofaqueueusingstacks.​​push(x)​​​–Pushelementxtothebacko......
  • LeetCode: 227. Basic Calculator II
    LeetCode:227.BasicCalculatorII题目描述Implementabasiccalculatortoevaluateasimpleexpressionstring.Theexpressionstringcontainsonlynon-negative......
  • LeetCode: 239. Sliding Window Maximum
    LeetCode:239.SlidingWindowMaximum题目描述Givenanarraynums,thereisaslidingwindowofsizekwhichismovingfromtheveryleftofthearraytotheve......
  • LeetCode: 228. Summary Ranges
    LeetCode:228.SummaryRanges题目描述Givenasortedintegerarraywithoutduplicates,returnthesummaryofitsranges.Example1:Input:[0,1,2,4,5,7]Output:[......