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

Leetcode DAY 49~50:买卖股票的最佳时机 1 2 3 4

  • 121. 买卖股票的最佳时机

1、贪心算法

class Solution {
public:int maxProfit(vector<int>& prices) {//贪心int low = INT_MAX;int res = 0;for(int i = 0; i < prices.size(); i++) {low = min(low, prices[i]); //左最小价格res = max(res, prices[i] - low); //当前-左最小的max}return res;}
};

2、动态规划

dp[i][0]表示第i天持有股票最大现金 -> max(前一天持有股票的最大现金, 前一天买入股票的现金)
dp[i][1]表示第i天不持有股票最大现金 -> max(前一天不持有股票最大现金, 前一天持有第i天买入的现金)

class Solution {
public:int maxProfit(vector<int>& prices) {vector<vector<int>> dp(prices.size(),vector<int>(2, 0));dp[0][0] = -prices[0];dp[0][1] = 0;for(int i = 1; i < prices.size(); i++){dp[i][0] = max(dp[i - 1][0], -prices[i]);dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);}return dp[prices.size() - 1][1];}
};

122.买卖股票的最佳时机II

1、dp

dp[i][0]表示第i天持有股票时的所的现金

dp[i][1]表示第i天不持有股票所得的现金

dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i])

dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])

class Solution {
public:int maxProfit(vector<int>& prices) {int n = prices.size();vector<vector<int>> dp(n, vector<int>(2,0));dp[0][0] = -prices[0];dp[0][1] = 0;for(int i = 1; i < n; i++) {dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);}return dp[n - 1][1];}
};

2、其他

class Solution:def maxProfit(self, prices: List[int]) -> int:res = 0for i in range(1, len(prices)):if prices[i] - prices[i - 1] > 0:res += prices[i] - prices[i - 1]return res
  • 123.买卖股票的最佳时机III

!最多可以完成两笔交易!

1、递推公式

dp[i][0] 第一次持有的现金 <--- dp[i - 1][0]     - prices[i] 

dp[i][1] 第一次不持有的现金 <--- dp[i - 1][1]     dp[i - 1][0] + prices[i]

dp[i][2] 第二次持有的现金  <---  dp[i - 1][2]    dp[i - 1][1] - prices[i]

dp[i][3] 第二次不持有的现金 <--- dp[i - 1][3]    dp[i - 1][2] + prices[i]

2、初始化

dp[0][0] = -prices[0]

dp[0][1] = 0

dp[0][2] = -prices[0]

dp[0][3] = 0

class Solution {
public:int maxProfit(vector<int>& prices) {int n = prices.size();if(n == 0)return 0;vector<vector<int>> dp(n, vector<int>(4, 0));dp[0][0] = -prices[0];dp[0][2] = -prices[0];for(int i = 1; i < n; i++) {dp[i][0] = max(dp[i - 1][0], -prices[i]);dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] - prices[i]);dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i]);}return dp[n - 1][3];}
};
  • 188.买卖股票的最佳时机IV

通过上一题 类比得到

class Solution {
public:int maxProfit(int k, vector<int>& prices) {int n = prices.size();vector<vector<int>> dp(n, vector<int>(2 * k + 1, 0));for(int i = 1; i <= 2*k; i += 2){dp[0][i] = - prices[0];}for(int i = 1; i < n; i++) {for(int j = 1; j <= 2 * k; j++) {dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + (1 - 2 * (j % 2)) * prices[i]);//1 - 2 * (j % 2)用来区分 奇数和偶数}}return dp[n - 1][2 *k];}
};

http://www.lryc.cn/news/25328.html

相关文章:

  • Android Handler机制(二) Handler 实现原理
  • Elasticsearch教程(19) 详解mapping之keyword
  • LeetCode算法复杂度分析(时间复杂度空间复杂度)
  • Android OpenCV(七十三):吊打高斯模糊的StackBlur Android 实践
  • 4.排序算法之一:冒泡排序
  • python自学之《21天学通Python》(16)——第19章 用Pillow库处理图片
  • 发布依赖到maven仓库
  • Laravel-admin之自定义操作日志
  • 用Python做了一个法律查询小工具,非常好用
  • 工作篇:触摸屏原理介绍
  • Ep_操作系统面试题-操作系统的分类
  • iframe或document监听滚动事件不起作用
  • 基频估计算法简介
  • linux修改DNS 系统版本Kylin V10桌面版
  • 如何使用 AWS Lambda 运行 selenium
  • 认识Cesium旋转大小变量
  • 异响加持、吐槽声不断,小鹏G9难解困局
  • 【react】react18的学习
  • Ep_操作系统面试题-什么是线程,线程和进程的区别
  • 最流行的自动化测试工具,总有一款适合你(附部分教程)
  • Shell高级——进程替换vs管道
  • 国内有哪些支持定制化的低代码平台?
  • Altair 宣布将于3月举办 Future.Industry 2023 全球虚拟大会
  • react lazyLoad学习记录
  • 29 openEuler管理网络-配置网络绑定
  • RTT 全志D1s RDC2022纪念版开发板开箱使用分享与折腾记录
  • 24日常实习万得一面面径
  • MySQL的DML和DDL操作(1)
  • Kafka系列之:Kafka生产者和消费者
  • Linux进程间通信:信号量(一)