摘要: 1、leetcode70 爬楼梯 需要 n 阶才能到达楼顶,每次可以爬 1 或 2 个台阶。 转化为完全背包问题 背包容量:n 物品【物品可以反复使用】 价值:1、2 重量(所占背包容量):1 代码 class Solution { public int climbStairs(int n) { / 阅读全文
posted @ 2023-02-28 22:52 黄三七 阅读(27) 评论(0) 推荐(0)
摘要: 1、leetcode518 零钱兑换Ⅱ【完全背包应用】【求组合数】 class Solution { public int change(int amount, int[] coins) { int[] dp = new int[amount+1]; dp[0] = 1; for(int i=0; 阅读全文
posted @ 2023-02-28 00:01 黄三七 阅读(15) 评论(0) 推荐(0)
摘要: 1、leetcode1049 最后一块石头的重量Ⅱ 思路 尽量让石头分成重量相同的两堆,相撞之后剩下的石头最小 转化为01背包问题:从stones 数组中选择,凑成总和不超过sum/2的最大价值。 其中「重量」&「价值」均为数值本身。 代码 class Solution { public int l 阅读全文
posted @ 2023-02-28 00:01 黄三七 阅读(24) 评论(0) 推荐(0)
摘要: 1、leetcode416 分隔等和子集 转化为01背包问题 背包的体积为sum / 2 背包要放入的商品(集合里的元素)重量为 元素的数值,价值也为元素的数值 背包如果正好装满,说明找到了总和为 sum / 2 的子集。 背包中每一个元素是不可重复放入。 class Solution { publ 阅读全文
posted @ 2023-02-28 00:00 黄三七 阅读(19) 评论(0) 推荐(0)
摘要: 1、leetcode343 整数拆分 class Solution { public int integerBreak(int n) { // dp[i] : 对i分解得到的最大乘积 int[] dp = new int[n+1]; // 对0 1 分解无意义 => 对dp[0] dp[1] 初始化 阅读全文
posted @ 2023-02-28 00:00 黄三七 阅读(24) 评论(0) 推荐(0)
摘要: 1、leetcode62 不同路径 步骤 确定dp数组(dp table)以及下标的含义 dp[i] [j] : 从(0,0)到(i,j)的路径数量 确定递推公式 (i,j) 可由 (i-1,j)、(i,j-1)到达【已知:每次只能向下或者向右移动一步】 dp[i] [j] = dp[i-1] [j 阅读全文
posted @ 2023-02-22 23:26 黄三七 阅读(24) 评论(0) 推荐(0)
摘要: 1、leetcode738 单调递增的数字 思路 一旦出现strNum[i - 1] > strNum[i]的情况(非单调递增),首先想让strNum[i - 1]-- 从后向前遍历,就可以重复利用上次比较得出的结果了 代码 class Solution { public int monotoneI 阅读全文
posted @ 2023-02-21 23:58 黄三七 阅读(18) 评论(0) 推荐(0)
摘要: 1、动规理论基础 动态规划中每一个状态一定是由上一个状态推导出来的,而贪心是局部直接选最优的。 解题步骤 确定dp数组(dp table)以及下标的含义 确定递推公式 dp数组如何初始化 确定遍历顺序 举例推导dp数组 2、leetcode509 斐波那契数 思路 确定dp数组(dp table)以 阅读全文
posted @ 2023-02-21 23:58 黄三七 阅读(23) 评论(0) 推荐(0)
摘要: 1、leetcode435 无重叠区间 代码 class Solution { public int eraseOverlapIntervals(int[][] intervals) { Arrays.sort(intervals,(a,b)->(a[0]-b[0])); int count = 1 阅读全文
posted @ 2023-02-19 19:47 黄三七 阅读(22) 评论(0) 推荐(0)
摘要: 1、leetcode860 柠檬水找零 class Solution { public boolean lemonadeChange(int[] bills) { if(bills[0] != 5) { return false; } int five_nums = 0; int ten_nums 阅读全文
posted @ 2023-02-19 19:46 黄三七 阅读(24) 评论(0) 推荐(0)