上一页 1 2 3 4 5 6 7 8 9 10 ··· 21 下一页
摘要: https://leetcode.cn/problems/word-break/description/ class Solution { public boolean wordBreak(String s, List<String> wordDict) { // 思路较为巧妙,和传统背包定义不同 阅读全文
posted @ 2024-09-04 19:14 风乐 阅读(21) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/perfect-squares/description/ 简单完全背包,需要注意的是由于求的是最小,因此初始化时需要把初始层f[0]全置为无穷大,用于保证一定能计算出min具体可以看灵神的解释 递归边界:dfs(0,0)=0,因为没有数可以选 阅读全文
posted @ 2024-09-04 02:48 风乐 阅读(33) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/coin-change/description/ 代码上比较麻烦的dp题,由于求的是最少数量,因此求答案时需要初始化无穷大来计算 class Solution { public int coinChange(int[] coins, int 阅读全文
posted @ 2024-09-03 21:01 风乐 阅读(15) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/combination-sum-iv/description/ 此篇题解解释了为什么不能直接用二维完全背包的方式做不过还是建议把这个题当成一个爬楼梯来做 class Solution { public: int combinationSum4 阅读全文
posted @ 2024-09-03 19:35 风乐 阅读(16) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/coin-change-ii/description/可以直接考虑用完全背包的传统二维做法,但是这里求的是组合数,需要注意 在求装满背包有几种方案的时候,认清遍历顺序是非常关键的。 如果求组合数就是外层for循环遍历物品,内层for遍历背包。 阅读全文
posted @ 2024-09-03 16:55 风乐 阅读(9) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/ones-and-zeroes/solutions/ 多重体积的01背包,关键是需要想到把构造这个最长子集 等价为 往一个背包里塞物品,求能塞最多的物品是多少?且这里有两层体积这样想就能转化为01背包了,即f[i][j][k]表示在前i个物品 阅读全文
posted @ 2024-09-03 03:19 风乐 阅读(27) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/target-sum/solutions/2119041/jiao-ni-yi-bu-bu-si-kao-dong-tai-gui-hua-s1cx/ 灵神的代码实现比我自己写的更好,可以多学习学习这道题的关键点在于想到 正数和+负数和=ta 阅读全文
posted @ 2024-09-03 02:15 风乐 阅读(29) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/last-stone-weight-ii/description/ 思路较为巧妙的dp题,关键点在于如何将问题转化为01背包,有点贪心的思想主要是划分为两堆尽可能相等的石碓,然后判断能否凑出这个偏小的石碓(若干石头中选,能否选出这个价值)这里 阅读全文
posted @ 2024-09-02 20:51 风乐 阅读(33) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/partition-equal-subset-sum/description/ 01背包问题,需要考虑到如何把这个问题转化成01背包问题转换成01背包问题后,如何定义f[i]状态来表示 这里有两种方式:1.按照传统01背包表示,即前i个物品中 阅读全文
posted @ 2024-09-01 20:22 风乐 阅读(16) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/unique-binary-search-trees/solutions/329807/bu-tong-de-er-cha-sou-suo-shu-by-leetcode-solution/ 较为困难的一道DP题,需要分析各个情况,以及想到如 阅读全文
posted @ 2024-09-01 00:10 风乐 阅读(19) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 7 8 9 10 ··· 21 下一页