最佳时间买卖股票问题在leetcode中出现了很多次,并且这些题目之间也具有相似性,所以我将它们做成一个专题来进行总结。

121. Best Time to Buy and Sell Stock

题目:

Say you have an array for which the ithi^thith element is the price of a given stock on day iii.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

分析:

这是一道比较简单的编程题,也是这系列题中的一道入门题。题目大意是只准完成一项交易,求利润最大值。我们需要用到两个变量release1和hold1,其中release1代表的是当前利润最大值;hold1代表的是当前股票最低价。这样就可以用动态规划来求解。

代码(c++):

class Solution {public:int maxProfit(vector<int>& prices) {int hold1 = INT_MIN, release1 = 0;for(int i : prices) {release1 = max(release1, hold1 + i);hold1 = max(hold1, -i);}return release1;}
};

122. Best Time to Buy and Sell Stock II

题目:

Say you have an array for which the ithi^thith element is the price of a given stock on day iii.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

分析:

这道题在第一题的基础上不限制交易次数。没有交易次数的限制后,只要比前一天股票价格高就是盈利。

代码(c++):

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

123. Best Time to Buy and Sell Stock III

题目:

Say you have an array for which the ithi^thith element is the price of a given stock on day iii.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

分析:

这道题也是和第一题的思路类似,区别在于第一题是只能交易一次,这道题是最多交易两次。我们需要用到四个变量release1、release2和hold1、hold1,其中release1、hold1和第一题含义一样;hold2的含义就是在交易一次的基础上再买股票的最佳入手时机、release2则表示交易两次的最大利润。用动态规划来求解。

代码(c++):

class Solution {public:int maxProfit(vector<int>& prices) {int hold1 = INT_MIN, hold2 = INT_MIN;int release1 = 0, release2 = 0;for(int i:prices){release2 = max(release2, hold2+i);hold2    = max(hold2,    release1-i);release1 = max(release1, hold1+i);hold1    = max(hold1,    -i); }return release2;}
};

188. Best Time to Buy and Sell Stock IV

题目:

Say you have an array for which the ithi^thith element is the price of a given stock on day iii.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

分析:

是第三题的一个推广,但是提交的时候可能会出现一个内存溢出的错误。原因是k值远远大于prices大小,这样用dp就不合理了。所以需要结合第二题的思路来进行解决。

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

class Solution {public:int maxProfit(int k, vector<int>& prices) {if(k == 0) return 0;if(k >= prices.size()) {int profit = 0;for(int i = 1; i < prices.size(); i++) {if(prices[i] > prices[i-1]) profit = profit + (prices[i] - prices[i-1]);}return profit;}vector<int> hold(k, INT_MIN);vector<int> release(k, 0);for(int i : prices){for(int j = k - 1; j >= 1; j--) {release[j] = max(release[j], hold[j] + i);hold[j] = max(hold[j], release[j - 1] - i);}release[0] = max(release[0], hold[0] + i);hold[0] = max(hold[0], -i); }return release[k-1];}
};

leetcode Best Time to Buy and Sell Stock专题相关推荐

  1. leetcode: Best Time to Buy and Sell Stock 系列

    leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...

  2. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

  3. Leetcode Best Time to Buy and Sell Stock

    Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...

  4. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

  5. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. [leetcode]_Best Time to Buy and Sell Stock I II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  7. LeetCode Best Time to Buy and Sell Stock(dp)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. LeetCode Best Time to Buy and Sell Stock with Cooldown(动态规划)

    问题:给定一个整数数组,其中第i个元素表示第i天的股票价格.可以多次买卖股票,卖出股票后,无法在第二天买入股票. 思路:动态规划法,用dp(i,j)表示第i天持有或者不持有股票的最大利润.用dp(i, ...

  9. [leetcode]Best Time to Buy and Sell Stock III

    先说思路.参考了这篇:http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-a ...

  10. Leetcode Best Time to Buy and Sell Stock IV(最大子段和)

    朴素的O(n^2)过不了,这里借鉴前人的思想,总结一下. 主要利用栈维护对后面产生影响的字段,然后利用贪心的思想逐步合并. 首先,找到每一对最低点(vx),最高点(px); 其次,合并当前(vx, p ...

最新文章

  1. 需求的推动力-网线啥的
  2. 修改mysql存储过程的权限调用权限 definer invoker
  3. 如何停止一个正在运行的java线程
  4. 天池实验室|读取数据集的两种方式
  5. net 调用java_NET调用Java之100-Continue的坑
  6. python快速上手_Python快速上手(一)
  7. Sql分页存储过程(支持多表分页存储)
  8. 自己做量化交易软件(45)小白量化实战18--直接使用通达信自编指标公式进行分析绘图和回测
  9. 【转自52v6】教你如何走IPv6下载百…
  10. mix2线刷开发板救砖_小米MIX2官方原厂系统rom线刷刷机包降级包下载8.10.25版
  11. Python 之 解析xml
  12. 碧桂园博智林机器人总部大楼_碧桂园11.4亿元竞得北滘坤洲地块,近博智林机器人谷...
  13. 如何成为一名优秀的技术工程师?
  14. 电商产品精修训练营第3天_ps抠图
  15. 性能强大的家庭服务器,家庭服务器解决方案——硬件篇
  16. 服务器限制网页只能跳转过来,限制网页只能在微信打开
  17. 组合数的几种计算方法
  18. IT项目经理应该做什么
  19. 学习笔记:LR语法分析
  20. 【2022第十三届蓝桥杯】c/c++ 大学c组 解题报告

热门文章

  1. sql server动态分页
  2. 实现CTF智能合约题目的环境部署
  3. sql server 2016新特性 查询存储(Query Store)的性能影响
  4. Oracle中的rownum不能使用大于的问题
  5. [LeetCode] Trapping Rain Water II 题解
  6. 2016.8.23 项目总结
  7. [转]解决Sublime Text 2中文显示乱码问题
  8. javascript 计算每行中的列数
  9. [测试模式]Setup方法的滥用
  10. 聊聊我的 ACL2020 论文