Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

322. 零钱兑换 #55

Open
Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

题目仅要求出最少硬币数量,无须考虑硬币的组合和排列,所以不用考虑两个 for 循环的内外顺序。

状态定义

dp[i]:凑足总额为 i 所需钱币的最少个数为 dp[i]

状态转移方程

dp[i] = min(dp[j - coins[j]] + 1, dp[i])

理解

不考虑第 j 个硬币时, 硬币数为dp[i]

考虑第 j 个硬币时,硬币数为dp[i - coins[j]] + 1

初始化

凑足总金额为 0 所需钱币的个数是 0,所以dp[0] = 0

下标非 0 元素应该为最大值:Number.MAX_VALUE

constcoinChange=function(coins,amount){constdp=Array(amount+1).fill(Number.MAX_VALUE)dp[0]=0for(leti=1;i<dp.length;i++){for(letj=0;j<coins.length;j++){if(i-coins[j]>=0){dp[i]=Math.min(dp[i],dp[i-coins[j]]+1)}}}returndp[dp.length-1]===Number.MAX_VALUE ?-1 :dp[dp.length-1]}
  • 时间复杂度: O(len(coins) * amount)
  • 空间复杂度: O(amount)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp