1、

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

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

给一个数prices[],prices[i]代表股票在第i天的售价,求出只做一次交易(一次买入和卖出)能得到的最大收益。

只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o....i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。

  1. class Solution {
  2. public:
  3. int maxProfit(vector<int> &prices) {
  4. if(prices.empty())
  5. return ;
  6. int res=,min=prices[];
  7. for(int i=;i<prices.size();i++){
  8. if(prices[i]<min) min=prices[i];
  9. else if(prices[i]-min>res) res=prices[i]-min;
  10. }
  11. return res;
  12. }
  13. };

2、

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

此题和上面一题的不同之处在于不限制交易次数。也是一次遍历即可,只要可以赚就做交易。

  1. class Solution {
  2. public:
  3. int maxProfit(vector<int> &prices) {
  4. if(prices.empty()) return ;
  5. int res =;
  6. for(int i=;i<prices.size();i++){
  7. if(prices[i]-prices[i-]>)
  8. res+=prices[i]-prices[i-];
  9. }
  10. return res;
  11. }
  12. };

3、

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

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

此题是限制在两次交易内,相对要难一些。容易想到的解决办法是,把prices[] 分成两部分prices[0...m] 和 prices[m...length]  ,分别计算在这两部分内做交易的做大收益。由于要做n次划分,每次划分可以采用 第一题:  I的解法, 总的时间复杂度为O(n^2).

  1. public class Solution {
  2. public int maxProfit(int[] prices) {
  3. int ans = 0;
  4. for(int m = 0; m<prices.length; m++){
  5. int tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
  6. if(tmp > ans) ans = tmp;
  7. }
  8. return ans;
  9. }
  10.  
  11. public int maxProfitOnce(int[] prices,int start, int end){
  12. if(start >= end) return 0;
  13. int low = prices[start];
  14. int ans = 0;
  15. for(int i=start+1; i<=end; i++){
  16. if(prices[i] < low) low = prices[start];
  17. else if(prices[i] - low > ans) ans = prices[i] - low;
  18. }
  19. return ans;
  20. }
  21.  
  22. }

但是由于效率过低,运行超时。可以利用动态规划的思想进行改进,保持计算的中间结果,减少重复的计算。

那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。计算方法也是利用第一个问题的计算方法。 第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。 所以最后算法的复杂度就是O(n)的。

就是说,通过预处理,把上面的maxProfitOnce()函数的复杂度降到O(1)

  1. class Solution {
  2. public:
  3. int maxProfit(vector<int> &prices) {
  4. if(prices.empty()) return ;
  5. int n=prices.size();
  6. vector<int> opt(n,);
  7. int res=,low=prices[];
  8. for(int i=;i<n;i++){
  9. if(prices[i]<low) low=prices[i];
  10. else if(res <prices[i]-low) res=prices[i]-low;
  11. opt[i]=res;
  12. }
  13. vector<int> optReverse(n,);
  14. int high=prices[n-];
  15. res=;
  16. for(int i=n-;i>=;i--){
  17. if(prices[i]>high) high=prices[i];
  18. else if(high-prices[i]>res) res=high-prices[i];
  19. optReverse[i]=res;
  20. }
  21. res=;
  22.  
  23. for(int i=;i<n;i++){
  24. int tmp=opt[i]+optReverse[i];
  25. res=tmp>res?tmp:res;
  26. }
  27. return res;
  28. }
  29. };

best-time-to-buy-and-sell-stock系列——先买入后卖出股票的最大值的更多相关文章

  1. Best Time to Buy and Sell Stock系列

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

  2. LeetCode -- Best Time to Buy and Sell Stock系列

    Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...

  3. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  4. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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

  5. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

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

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

  7. Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum

    这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...

  8. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  9. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

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

随机推荐

  1. python--eclipse第一步总结

    1.SyntaxError: Non-UTF-8 code starting with '\xc4' in file C:\Users\yangqiong\workspace\create.报错 解决 ...

  2. kNN的维数灾难与PCA降维

    主成分分析 PCA 协方差矩阵 假设我们有 \[ X = \begin{pmatrix}X_1\\X_2\\\vdots\\X_m\end{pmatrix}\in\mathbb{R}^{m\times ...

  3. 【bzoj3751】[NOIP2014]解方程 数论

    题目描述 已知多项式方程: a0+a1*x+a2*x^2+...+an*x^n=0 求这个方程在[1,m]内的整数解(n和m均为正整数). 输入 第一行包含2个整数n.m,每两个整数之间用一个空格隔开 ...

  4. 【Luogu】P1411树(树形高精DP)

    题目链接 我貌似又做了一道高精题呢(笑) 这题的DP方程很好想,设f[i][j]表示i为根的子树,i所在联通块大小为j的最大值,然后乱搞 但是要高精,那么搞是得要高精除的 所以考虑f[i][j]是除以 ...

  5. [luoguP2569] [SCOI2010]股票交易(DP + 单调队列)

    传送门 $f[i][j]$ 表示第i天,手中股票数为j的最优解 初始化 $f[i][0]=0$ $0<=i<=n$ 4种方式转移 以前没买过,第i天凭空买 $f[i][j]=-j*ap$ ...

  6. 关于sql语句实现将'1,2,3'转1,2,3等竖横转换问题

    这是本人的第一个博客,以后会及时整理遇到的问题,方便和大家进行交流. 第一次也不知道说什么了,直接进入正题吧. 因为我的数据在设计时候数据源是竖列存的,满足条件的数据是横列存的.如下图所示: 我要筛选 ...

  7. APUE 学习笔记(二) 文件I/O

    1. 文件I/O 对于内核而言,所有打开的文件都通过文件描述符引用,内核不区分文本文件和二进制文件 open函数:O_RDONLY  O_WRONLY  O_RDWR create函数: close函 ...

  8. net4:MultiView(view)行为中的commmandname与commmandargument,ListBox的使用及移动操作

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  9. mybatis原理源码大牛连接

    mybatis讲解的非常好的连接: https://www.jianshu.com/nb/5226994 执行流程简介参考:http://www.cnblogs.com/dongying/p/4142 ...

  10. 安装apache2.4和php7.1的方法记录

    下载扩展 这是PHP官方扩展下载的地址 http://pecl.php.net 点击下载dll文件,放到php的ext目录. 看这个说明下载你PHP版本支持的版本.