摘要:
121. 买卖股票的最佳时机 1、动态规划 class Solution: def maxProfit(self, prices: List[int]) -> int: # dp[i][0] 代表第 i 天持有股票获取的最大利益 # dp[i][1] 代表第 i 天不持有股票获取的最大利益 dp = 阅读全文
摘要:
198.打家劫舍 1、动态规划 class Solution: def rob(self, nums: List[int]) -> int: # dp 数组代表在第 i 个房间可以偷窃到的最高金额为 dp[i] dp = [0] * len(nums) if len(nums) == 1: retu 阅读全文
摘要:
139.单词拆分 class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: dp = [False] * (len(s) + 1) dp[0] = True # 求排列先遍历背包再遍历物品 for i in r 阅读全文
摘要:
70. 爬楼梯 (进阶) 1、使用 01 背包解法 class Solution: def climbStairs(self, n: int) -> int: # dp 数组代表爬上第 i 阶有 dp[j] 种方法 dp = [0] * (n + 1) dp[0] = 1 m = 2 # 排列先背包 阅读全文
摘要:
[完全背包] 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品都有无限个(也就是可以放入背包多次),求解将哪些物品装入背包里物品价值总和最大。 1、先遍历物品再遍历背包 def all_bag(weight, value, bag 阅读全文