from typing import List class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [amount+1] * (amount+1) # set all maximum dp[0] = 0 for a in range(1, amount+1): for c in coins: if a-c >= 0: dp[a]= min(dp[a], 1+dp[a-c]) # 1:original amount return -1 if dp[amount] == amount+1 else dp[amount] # dp[amount] + 1 is default
class Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: if not candidates: return [] res = [] candidates.sort() def dfs(idx, path, cur): # 開始加總位置, 建構可能解,目前的值 if cur > target: return # less than target if cur == target: # same as target res.append(path) return for i in range(idx, len(candidates)): dfs(i, path+[candidates[i]], cur+candidates[i]) dfs(0, [], 0) return res