摘要: 今天是第四十九天,用动态规划的思想去解决之前用贪心算法解决的股票买卖问题 121. 买卖股票的最佳时机 class Solution { public int maxProfit(int[] prices) { int n = prices.length; if(n == 1){ return 0; 阅读全文
posted @ 2022-11-30 16:15 小猫Soda 阅读(17) 评论(0) 推荐(0)
摘要: 今天是训练营的第四十八天,开始了动态规划的强盗问题 198. 打家劫舍 class Solution { public int rob(int[] nums) { int n = nums.length; if(n==1){ return nums[0]; } int[] dp = new int[ 阅读全文
posted @ 2022-11-30 03:49 小猫Soda 阅读(21) 评论(0) 推荐(0)
摘要: 今天是第四十六天,动态规划周的最后一天 139. 单词拆分 class Solution { public boolean wordBreak(String s, List<String> wordDict) { int n = s.length(); boolean[] dp = new bool 阅读全文
posted @ 2022-11-27 06:22 小猫Soda 阅读(20) 评论(0) 推荐(0)
摘要: 今天是第四十五天,继续动态规划 70. 爬楼梯 (进阶) class Solution { public int climbStairs(int n) { int[] dp = new int[n+1]; dp[0] = 1; for(int i = 0; i<=n; i++){ for(int j 阅读全文
posted @ 2022-11-26 16:19 小猫Soda 阅读(16) 评论(0) 推荐(0)
摘要: 今天是第四十四天,继续动态规划 518. 零钱兑换 II class Solution { public int change(int amount, int[] coins) { int n = coins.length; int[] dp = new int[amount+1]; dp[0] = 阅读全文
posted @ 2022-11-25 15:32 小猫Soda 阅读(18) 评论(0) 推荐(0)
摘要: 今天是第四十三天,还是背包问题 1049. 最后一块石头的重量 II class Solution { public int lastStoneWeightII(int[] stones) { int n = stones.length; int sum = 0; for (int i : ston 阅读全文
posted @ 2022-11-24 13:29 小猫Soda 阅读(19) 评论(0) 推荐(0)
摘要: 今天是第四十二天,是动态规划中的背包问题 416. 分割等和子集 class Solution { public boolean canPartition(int[] nums) { int sum = 0; int n = nums.length; int[] dp = new int[10001 阅读全文
posted @ 2022-11-23 11:58 小猫Soda 阅读(17) 评论(0) 推荐(0)
摘要: 今天是四十一天,从今天起难度大概就要上来了 343.整数拆分 class Solution { public int integerBreak(int n) { int[] dp = new int[n+1]; dp[2] = 1; for(int i = 3; i<=n; i++){ for(in 阅读全文
posted @ 2022-11-21 02:27 小猫Soda 阅读(19) 评论(0) 推荐(0)
摘要: 今天是第三十九天,只有两道动态规划的题 62.不同路径 class Solution { public int uniquePaths(int m, int n) { int[][] path = new int[m][n]; for(int i = 0; i<m; i++){ path[i][0] 阅读全文
posted @ 2022-11-20 01:48 小猫Soda 阅读(21) 评论(0) 推荐(0)
摘要: 今天是第三十八天,最难的动态规划要开始了 509. 斐波那契数 class Solution { public int fib(int n) { if(n < 2){ return n; }int[] fibNum = new int[n+1]; fibNum[0] = 0; fibNum[1] = 阅读全文
posted @ 2022-11-19 12:10 小猫Soda 阅读(23) 评论(0) 推荐(0)