上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 36 下一页
摘要: 完全背包 import java.util.Arrays; class Solution { public int coinChange(int[] coins, int amount) { /** * dp[j]定义为总金额为j时最少的硬币数量 * 因为是求最小值,因此所有位置初始化为最大值 * 阅读全文
posted @ 2022-01-26 14:30 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要: 完全背包 class Solution { public int combinationSum4(int[] nums, int target) { /** * dp[i]定义为总和为i时排列的数量 * 初始dp[0] == 1 */ int[] dp = new int[target + 1]; 阅读全文
posted @ 2022-01-26 11:04 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要: 完全背包 class Solution { public int change(int amount, int[] coins) { /** * dp[j]定义为总金额为j时组合的方式数量 * 初始dp[0] == 1 */ int[] dp = new int[amount + 1]; dp[0] 阅读全文
posted @ 2022-01-26 10:33 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要: 01背包 class Solution { public int findMaxForm(String[] strs, int m, int n) { /** * 这个背包有两个维度,一个是m 一个是n,物品就是字符串,重量就是字符串中的01个数,价值就是字符串个数,都是1 * dp[i][j]定义 阅读全文
posted @ 2022-01-25 11:12 振袖秋枫问红叶 阅读(59) 评论(0) 推荐(0)
摘要: 01背包 import java.util.Arrays; class Solution { public int findTargetSumWays(int[] nums, int target) { int sum = Arrays.stream(nums).sum(); /** * 可以将数组 阅读全文
posted @ 2022-01-25 09:46 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要: 01背包 import java.util.Arrays; class Solution { public int lastStoneWeightII(int[] stones) { /** * 题意可以理解为,将石头尽可能分成相等的两份,最后差值就是最小剩余重量 * 剩下的部分和《416. 分割等 阅读全文
posted @ 2022-01-24 21:39 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要: 01背包 import java.util.Arrays; class Solution { public boolean canPartition(int[] nums) { int sum = Arrays.stream(nums).sum(); /** * 如果所有数的和为奇数,则不能平分 * 阅读全文
posted @ 2022-01-24 17:06 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int rob(TreeNode root) { int[] dp = robOrNot(root); return Math.max(dp[0], dp[1]); } /** * 对于每个节点来说,都有两个孩子,无法用一个dp[j]同时表达 阅读全文
posted @ 2022-01-21 23:33 振袖秋枫问红叶 阅读(44) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int rob(int[] nums) { if (nums.length == 1){ return nums[0]; } int[] dp = new int[nums.length]; int max = 0; /** * 重复两次《1 阅读全文
posted @ 2022-01-21 19:49 振袖秋枫问红叶 阅读(95) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int rob(int[] nums) { if (nums.length == 1){ return nums[0]; } /** * dp[i]定义为到达第i个房屋时总共可以偷到的最大金额 * 到第一个房屋时只能偷到其本身,到第二个房屋时 阅读全文
posted @ 2022-01-21 18:37 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 36 下一页