当前位置: 首页 > news >正文

代码随想录算法训练营day41|动态规划part08

第一题:121. Best Time to Buy and Sell Stock

贪心法:

class Solution {public int maxProfit(int[] prices) {// 找到一个最小的购入点int low = Integer.MAX_VALUE;// res不断更新,直到数组循环完毕int res = 0;for(int i = 0; i < prices.length; i++){low = Math.min(prices[i], low);res = Math.max(prices[i] - low, res);}return res;}
}

动态规划:版本一

// 解法1
class Solution {public int maxProfit(int[] prices) {if (prices == null || prices.length == 0) return 0;int length = prices.length;// dp[i][0]代表第i天持有股票的最大收益// dp[i][1]代表第i天不持有股票的最大收益int[][] dp = new int[length][2];int result = 0;dp[0][0] = -prices[0];dp[0][1] = 0;for (int i = 1; i < length; i++) {dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);}return dp[length - 1][1];}
}

动态规划:版本二(使用二維數組(和卡哥思路一致),下面還有使用一維滾動數組的更優化版本)

class Solution {public int maxProfit(int[] prices) {int len = prices.length;int dp[][] = new int[2][2];dp[0][0] = - prices[0];dp[0][1] = 0;for (int i = 1; i < len; i++){dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], - prices[i]);dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], prices[i] + dp[(i - 1) % 2][0]);}return dp[(len - 1) % 2][1];}
}

动态规划:版本二(使用一維數組)

class Solution {public int maxProfit(int[] prices) {int[] dp = new int[2];// 记录一次交易,一次交易有买入卖出两种状态// 0代表持有,1代表卖出dp[0] = -prices[0];dp[1] = 0;// 可以参考斐波那契问题的优化方式// 我们从 i=1 开始遍历数组,一共有 prices.length 天,// 所以是 i<=prices.lengthfor (int i = 1; i <= prices.length; i++) {// 前一天持有;或当天买入dp[0] = Math.max(dp[0], -prices[i - 1]);// 如果 dp[0] 被更新,那么 dp[1] 肯定会被更新为正数的 dp[1]// 而不是 dp[0]+prices[i-1]==0 的0,// 所以这里使用会改变的dp[0]也是可以的// 当然 dp[1] 初始值为 0 ,被更新成 0 也没影响// 前一天卖出;或当天卖出, 当天要卖出,得前一天持有才行dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);}return dp[1];}
}

第二题:122. Best Time to Buy and Sell Stock II

// 动态规划
class Solution // 实现1:二维数组存储// 可以将每天持有与否的情况分别用 dp[i][0] 和 dp[i][1] 来进行存储// 时间复杂度:O(n),空间复杂度:O(n)public int maxProfit(int[] prices) {int n = prices.length;int[][] dp = new int[n][2];     // 创建二维数组存储状态dp[0][0] = 0;                   // 初始状态dp[0][1] = -prices[0];for (int i = 1; i < n; ++i) {dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);    // 第 i 天,没有股票dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);    // 第 i 天,持有股票}return dp[n - 1][0];    // 卖出股票收益高于持有股票收益,因此取[0]}
}
//DP using 2*2 Array (下方還有使用一維滾動數組的更優化版本)
class Solution {public int maxProfit(int[] prices) {int dp[][] = new int [2][2];//dp[i][0]: holding the stock//dp[i][1]: not holding the stockdp[0][0] = - prices[0];dp[0][1] = 0;for(int i = 1; i < prices.length; i++){dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]);dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]);}return dp[(prices.length - 1) % 2][1];}
}
// 优化空间
class Solution {public int maxProfit(int[] prices) {int[] dp = new int[2];// 0表示持有,1表示卖出dp[0] = -prices[0];dp[1] = 0;for(int i = 1; i <= prices.length; i++){// 前一天持有; 既然不限制交易次数,那么再次买股票时,要加上之前的收益dp[0] = Math.max(dp[0], dp[1] - prices[i-1]);// 前一天卖出; 或当天卖出,当天卖出,得先持有dp[1] = Math.max(dp[1], dp[0] + prices[i-1]);}return dp[1];}
}

 

第三题:123. Best Time to Buy and Sell Stock III 

// 版本一
class Solution {public int maxProfit(int[] prices) {int len = prices.length;// 边界判断, 题目中 length >= 1, 所以可省去if (prices.length == 0) return 0;/** 定义 5 种状态:* 0: 没有操作, 1: 第一次买入, 2: 第一次卖出, 3: 第二次买入, 4: 第二次卖出*/int[][] dp = new int[len][5];dp[0][1] = -prices[0];// 初始化第二次买入的状态是确保 最后结果是最多两次买卖的最大利润dp[0][3] = -prices[0];for (int i = 1; i < len; i++) {dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);}return dp[len - 1][4];}
}// 版本二: 空间优化
class Solution {public int maxProfit(int[] prices) {int[] dp = new int[4]; // 存储两次交易的状态就行了// dp[0]代表第一次交易的买入dp[0] = -prices[0];// dp[1]代表第一次交易的卖出dp[1] = 0;// dp[2]代表第二次交易的买入dp[2] = -prices[0];// dp[3]代表第二次交易的卖出dp[3] = 0;for(int i = 1; i <= prices.length; i++){// 要么保持不变,要么没有就买,有了就卖dp[0] = Math.max(dp[0], -prices[i-1]);dp[1] = Math.max(dp[1], dp[0]+prices[i-1]);// 这已经是第二次交易了,所以得加上前一次交易卖出去的收获dp[2] = Math.max(dp[2], dp[1]-prices[i-1]);dp[3] = Math.max(dp[3], dp[2]+ prices[i-1]);}return dp[3];}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【Unity打包Android】Gradle报错,Deprecated Gradle features were used in this build ···
  • Windows的cmd命令行使用Linux类命令
  • C语言memcmp函数
  • c++中加不加const的值传递和引用传递的区别
  • How to import openai package using jupyter notebook?
  • Dav_笔记13:SQL Access Advisor 之 2 使用SQL Access Advisor-3
  • Linux-Shell三剑客grep,awk,sed-08
  • 基于STM32设计的智能鱼缸(华为云IOT)(200)
  • stm32—时钟、定时器和看门狗
  • 代码随想录第38天|完全背包
  • mybatis常见面试问题
  • Cannot connect to the Docker daemon at unix:///var/run/docker.sock. 问题解决
  • Docker最佳实践进阶(一):Dockerfile介绍使用
  • 详解贪心算法
  • CANopen 控制多台设备的支持能力与定制方案评估
  • css系列之关于字体的事
  • interface和setter,getter
  • JavaScript类型识别
  • JSDuck 与 AngularJS 融合技巧
  • Js基础知识(四) - js运行原理与机制
  • magento 货币换算
  • MobX
  • nfs客户端进程变D,延伸linux的lock
  • spring学习第二天
  • vue2.0项目引入element-ui
  • 从重复到重用
  • 深度学习中的信息论知识详解
  • 阿里云ACE认证学习知识点梳理
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • #NOIP 2014# day.1 T2 联合权值
  • #pragma 指令
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (pojstep1.1.2)2654(直叙式模拟)
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (二)c52学习之旅-简单了解单片机
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (算法)Game
  • (算法)区间调度问题
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转载)Google Chrome调试JS
  • .gitignore文件—git忽略文件
  • .htaccess配置常用技巧
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .NET 给NuGet包添加Readme
  • .net 后台导出excel ,word
  • .NET 设计一套高性能的弱事件机制
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .net与java建立WebService再互相调用
  • /run/containerd/containerd.sock connect: connection refused
  • @Autowired 与@Resource的区别