这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富?

best time to buy and sell stock i:

最多允许买卖一次

best time to buy and sell stock ii:

不限制买卖次数

best time to buy and sell stock iii:
最多允许买卖两次

对于i:

  思路就是在第i个位置抛出时,希望在0~i-1个位置上的最低价买入,才能使得第i个位置的收益最大。然后比较所有可以抛出的位置i,返回最大的收益。

int maxP = 0, minV = prices[0];

for(int i = 1;i<prices.size();i++){
maxP = max(maxP,prices[i]-minV);
minV = min(minV,prices[i]);
} return maxP;

对于ii:

  由于可以无限买,所以思路就是只要赚钱我就卖掉。你可能会问,如果我在i天卖掉,发现i+1天能赚更多,怎么破?血亏!在线等!当然就是在第i天再买入,第i+1天再卖出,虽然规则上不允许同一天有多次买卖,但是这样其实等效于i-1天买入,i天划水,i+1天卖出。

int maxP = 0;

for(int i = 1;i<prices.size();i++){
if(prices[i]-prices[i-1] > 0) maxP+=(prices[i] - prices[i-1]);
}
return maxP;

对于iii:

  同样不需要额外的空间,而且只需要O(n)的空间复杂度。思路就是我一开始没有钱,buy1是我第一次买股票之后剩的钱(肯定是负的,我白手起家,借钱买股票啊),sell1是我卖完第一次买的股票之后剩的钱(>=0),buy2是我用上回挣得钱(有可能需要找别人再借一点)买第二次股票,sell2是我卖完第二次股票之后剩下的钱。

int buy1 = INT_MIN, sell1 = 0, buy2 = INT_MIN, sell2 = 0;

for(auto p:prices){
buy1 = max(buy1,-p);
sell1 = max(sell1,buy1+p);
buy2 = max(buy2,sell1-p);
sell2 = max(sell2, buy2+p);
} return sell2;

  

解题思路:best time to buy and sell stock i && ii && iii的更多相关文章

  1. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  2. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

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

  3. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

  4. LeetCode:Best Time to Buy and Sell Stock I II III

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

  5. Best Time to Buy and Sell Stock I II III

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

  6. Best Time to Buy and Sell Stock I,II,III [leetcode]

    Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...

  7. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

  8. Best Time to Buy and Sell Stock I II III IV

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

  9. LeetCode解题报告—— Best Time to Buy and Sell Stock

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

随机推荐

  1. SpringCloud接入EDAS——服务发现篇

    旁白 很久没有写技术文章了,最近不是写水文就是写小说.说到底,还是最近很少研究技术的缘故,已经到了江郎才尽的地步了. 不过,LZ无意间看到自己团队的小伙伴写的一些文章,觉得还是不错的,于是便动了心思, ...

  2. 常用接口简析2---IComparable和IComparer接口的简析

    常用接口的解析(链接) 1.IEnumerable深入解析 2.IEnumerable.IEnumerator接口解析 3.IList.IList接口解析 默认情况下,对象的Equals(object ...

  3. openface 训练数据集

    训练深度网络模型OpenFace还不是运用faceNet的model作为训练模型,所以在准确性上比faceNet要低,如果你只是做一个简单的分类,建议你看看官网的demo3(http://cmusat ...

  4. 使用 Bundle 在 Activity 之间交换数据

    [toc] 使用 Bundle 在 Activity 之间交换数据 场景 当一个 Activity 启动另一个 Activity 时,常常会有一些数据需要传过去.因为两个 Activity 之间本来就 ...

  5. mysql安装简单教程(自动安装/配置安装)

    mysql安装简单教程(自动安装/配置安装) 1.1前言: 由于特殊原因,在最近2-3个月里mysql真是安装了无数遍,每次安装都要上网找教程,每个教程基本都不一样,因此还是自己写下来比较好,毕竟自己 ...

  6. 计算机网络初探(ip协议)

    粗读了两遍计算机网络(谢希仁),对于计算计算机网络算是有了一个初步的了解,所以打算写一篇文章(希望是教程)进行巩固(主要围绕IP协议). 局域网 因特网的产生和广泛使用极大地改变了我们的生活,但对于不 ...

  7. NYOJ 323 Drainage Ditches 网络流 FF 练手

    Drainage Ditches 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...

  8. Java消息服务初步学习(基于Spring In Action的整理)

    几个名词 Java消息服务(Java Message Service)是一个Java标准,定义了使用消息代理的通用API. 消息代理(message broker):类似于邮局的作用,确保消息被投递到 ...

  9. Subquery returns more than 1 row

    Subquery returns more than 1 row表示子查询返回了多行数据 例如: select * from table1 where table1.colums=(select co ...

  10. javascript算法题判断输入年份是否是闰年

    用户输入一个年份,判断这个年是否是闰年.判断闰年条件:① 非整百年数除以4,无余为闰,有余不闰:② 整百年数除以400,无余为闰,有余不闰.比如:2000年,整百数年,就要用②公式,除以400,无余数 ...