视频讲解
代码
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
"""
Solution-1: Recurtsion with memory
"""
# mem = {}
# def helper(coins, amount) -> int:
# if amount in mem: return mem[amount]
# if amount == 0:
# mem[amount] = 0
# return 0
# res = sys.maxsize - 1
# for coin in coins:
# if amount >= coin:
# this_res = helper(coins, amount - coin)
# if this_res == -1:
# continue
# else:
# res = min(res, this_res + 1)
# if res == sys.maxsize-1:
# mem[amount] = -1
# return -1
# mem[amount] = res
# return res
# return helper(coins, amount)
"""
Solution-2: Dynamic Programming.
"""
dp = [sys.maxsize-1] * (amount + 1)
dp[0] = 0
for i in range(amount + 1):
if dp[i] == sys.maxsize-1:
continue
for coin in coins:
if coin + i <= amount:
dp[coin + i] = min(dp[coin+i], dp[i] + 1)
if dp[-1] == sys.maxsize-1: return -1
return dp[-1]
PPT讲解
本题涉及的知识点:
- [()][]
我的 Leetcode 讲解频道
代码链接
面试高频考点Youtube链接
- 数组(Array)
- 哈希表(Hash Table)
- 链表(Linked List)
- 数学(Math)
- 链表(Linked List)
- 双指针(Two Pointers)
- 字符串(String)
- 二分查找(Binary Search)
- 分治(Divide and Conquer)
- 动态规划(Dynamic Programming)
- 回溯(Backtracking)
- 栈(Stack)
- 堆(Heap)
- 贪心算法(Greedy)
- 排序(Sort)
- 树(Tree)
- 深度优先搜索(Depth-First Search)
- 广度优先搜索(Breadth-First Search)
- 二叉查找树(Binary Search Tree)
- 递归(Recursion)
- 队列(Queue)
- 移动窗口(Sliding Window)