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)。

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
return ;
int res=,min=prices[];
for(int i=;i<prices.size();i++){
if(prices[i]<min) min=prices[i];
else if(prices[i]-min>res) res=prices[i]-min;
}
return res;
}
};

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).

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

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

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).

 public class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for(int m = 0; m<prices.length; m++){
int tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
if(tmp > ans) ans = tmp;
}
return ans;
} public int maxProfitOnce(int[] prices,int start, int end){
if(start >= end) return 0;
int low = prices[start];
int ans = 0;
for(int i=start+1; i<=end; i++){
if(prices[i] < low) low = prices[start];
else if(prices[i] - low > ans) ans = prices[i] - low;
}
return ans;
} }

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

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

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

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int n=prices.size();
vector<int> opt(n,);
int res=,low=prices[];
for(int i=;i<n;i++){
if(prices[i]<low) low=prices[i];
else if(res <prices[i]-low) res=prices[i]-low;
opt[i]=res;
}
vector<int> optReverse(n,);
int high=prices[n-];
res=;
for(int i=n-;i>=;i--){
if(prices[i]>high) high=prices[i];
else if(high-prices[i]>res) res=high-prices[i];
optReverse[i]=res;
}
res=; for(int i=;i<n;i++){
int tmp=opt[i]+optReverse[i];
res=tmp>res?tmp:res;
}
return res;
}
};

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. Nginx从入门到放弃-第2章 基础篇

    2-1 什么是Nginx 2-2 常见的中间件服务 2-3 Nginx的特性_实现优点1 2-4 Nginx特性_实现优点2 2-5 Nginx特性_实现优点3 2-6 Nginx特性_实现优点4 2 ...

  2. 03-python进阶-爬虫入门-正则

    [urllib and urllib2] 这是两个python的网络模块 内置的 提供很好的网络访问的功能. #!coding:utf-8 import urllib2 res = urllib2.u ...

  3. matlab 初级画图

    matlab 初级画图 1.plot() plot(x,y)   plots each vector pairs (x,y) 画图函数画出每个点   每组变量 plot (y)   plots eac ...

  4. JAVA-字符串按指定长度换行

    可能有汉字的字符串按指定长度换行. public String getStringByEnter(int length, String string) throws Exception { for ( ...

  5. Vijos1512 SuperBrother打鼹鼠

    SuperBrother打鼹鼠 Vijos链接 题目描述: 在一个矩阵中,有三种操作: 1.后面跟着3个数x,y,k,表示在点(x,y)处新出现了k只鼹鼠. 2.后面跟着4个数x1,y1,x2,y2, ...

  6. 【转】PhpStorm 提交代码到远程GitHub仓库

    转载地址:http://my.oschina.net/lujianing/blog/180728 1.下载github for window http://windows.github.com/ 2. ...

  7. 漫谈登录桩(MockStub)的实现

    2014年6月4日,6月的第一个星期三,我正式入职百度,开始baiduer的工作.这不到2个月的时间,因为人力资源这边原因,我从INF部门离开,拉到了百度Hi-Server团队中来.2个完全不着调的岗 ...

  8. HDU-1853 Cyclic Tour

    最小权值环覆盖问题:用几个环把所有点覆盖,求所选取的边最小的权值之和. 拆点思想+求最小转求最大+KM算法 #include <cstdlib> #include <cstdio&g ...

  9. BZOJ 4816 [Sdoi2017]数字表格 ——莫比乌斯反演

    大力反演出奇迹. 然后xjb维护. 毕竟T1 #include <map> #include <ctime> #include <cmath> #include & ...

  10. LeetCode OJ--Unique Paths II **

    https://oj.leetcode.com/problems/unique-paths-ii/ 图的深搜,有障碍物,有的路径不通. 刚开始想的时候用组合数算,但是公式没有推导出来. 于是用了深搜, ...