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. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  2. 博客笔记(blog notebook)

    1. 机器学习 2. NLP 3. code 实际好人 实际坏人 预测百分比 预测好人 \(p_GF^c(s_c\|G)\) \(p_BF^c(s_c\|B)\) \(F^c(s_c)\) 预测坏人 ...

  3. maven无法下载依赖jar包—几种仓库的区别

    一.问题背景 最近这两天,感觉自己智商急剧退化,到了自己都捉急的地步,呃,有必要记录下来,以后智商被人甩几条街的时候,看看这篇文字,找找灵感也是好的! 这个项目呢,是用IDEA开发的,我一切都弄好了, ...

  4. mysqld got signal 11

    问题发生背景 问题实例之前使用的是percona server,是安装pmm镜像自带的数据库,之后通过mysqldump迁移到了MySQL server,目前是只有有pmm server 访问pmm库 ...

  5. CS231n笔记 Lecture 2 Image Classification pipeline

    距离度量\(L_1\) 和\(L_2\)的区别 一些感性的认识,\(L_1\)可能更适合一些结构化数据,即每个维度是有特别含义的,如雇员的年龄.工资水平等等:如果只是一个一般化的向量,\(L_2\)可 ...

  6. HDU——2093考试排名(string类及其函数的运用以及istringstream)

    考试排名 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. 算法复习——树形dp

    树形dp的状态转移分为两种,一种为从子节点到父节点,一种为父节点到子节点,下面主要讨论子节点到父亲节点的情况: 例题1(战略游戏): 这是一道典型的由子节点状态转移到父节点的问题,而且兄弟节点之间没有 ...

  8. vue学习:解决Apycharm的 * is only available in ES6(use 'esversion: 6') 问题

    使用pycharm打开main.js,代码前出现黄点,js报错了 把鼠标移至import的波浪线上,出现提示:W119 - ‘import’  is only available in ES6(use ...

  9. element-ui 的 upload组件的clearFiles方法调用方法

    <template> <div> <el-button @click="clearUploadedImage">重新上传</el-butt ...

  10. Linux System Programming 学习笔记(八) 文件和目录管理

    1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...