03 2022 档案

摘要:class Solution { public int maxProfit(int[] prices) { int len=prices.length; // 0: no stock; 1: hold stock; 2: no stock & in frozen stage int[] dp=new 阅读全文
posted @ 2022-03-27 15:51 livingsu 阅读(24) 评论(0) 推荐(0) 编辑
摘要:本质和买卖股票的最佳时机3一样。 class Solution { public int maxProfit(int k, int[] prices) { int len=prices.length; if(len==0||k==0) return 0; int k2=k*2; int[] dp=n 阅读全文
posted @ 2022-03-25 20:49 livingsu 阅读(22) 评论(0) 推荐(0) 编辑
摘要:dp[0]: 第i天已经完成1次买入的最大利润 dp[1]: 第i天已经完成1次买出的最大利润 dp[2]: 第i天已经完成2次买入的最大利润 dp[3]: 第i天已经完成2次买出的最大利润 class Solution { public int maxProfit(int[] prices) { 阅读全文
posted @ 2022-03-25 20:30 livingsu 阅读(17) 评论(0) 推荐(0) 编辑
摘要:不能简单用层次遍历把隔行的加起来,因为考虑单链表2->1->3->4,最大是2+4=6 f(node)表示占用node的最大值。g(node)表示不占用node的最大值。则: f(node)=node.val+g(node.left)+g(node.right) g(node)=max(f(node 阅读全文
posted @ 2022-03-24 22:20 livingsu 阅读(17) 评论(0) 推荐(0) 编辑
摘要:思路:因为第一个和最后一个不能同时选,所以将环形分成两段[0,len-2]和[1,len-1],分别用"打家劫舍1"求解,然后求最大值即可。 class Solution { public int rob(int[] nums) { int len=nums.length; if(len==1) r 阅读全文
posted @ 2022-03-24 21:35 livingsu 阅读(15) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示