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

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. 【Android开发学习笔记之一】5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  2. Bin、App_Data等文件夹详述(转自http://blog.csdn.net/zzjiadw/article/details/6801506)

    ASP.NET应用程序和ASP.Net网站所共有的文件: App_Browsers 包含 ASP.NET 用于标识个别浏览器并确定其功能的浏览器定义 (.browser) 文件.有关更多信息,请参见浏 ...

  3. 二、Hadoop学习笔记————架构学习

    1.成百上千台服务器组成集群,需要时刻检测服务器是否故障 2.用流读取数据更加高效快速 3.存储节点具有运算功能,省略了服务器之间来回传数据的网络带宽限制 4.一次写入,多次访问,不修改数据 5.多平 ...

  4. Tomcat 日志分割

    一.前言 随着每天业务的增长,Tomcat 的catalina.out日志 变得越来越大,占用磁盘空间不说.要查看某个时候的日志的时候,庞大的日志让你顿时无从下手,所以日志的切割的变得刻不容缓.而且, ...

  5. Azure IoT Edge on Raspberry Pi 3 with Raspbian

    在<Azure IoT Edge on Windows 10 IoT Core>一文中,我们以运行Windows 10 IoT Core的MinnowBoard MAX为例,详细讲述了Wi ...

  6. Java 代码学习之理解数据类型中的坑

    package dailytest; import org.junit.Test; public class DataTypeTest { /** * 当有字符串第一次参与运算后,+成了连接符的作用 ...

  7. .NET版支付宝商户会员卡接入

    最近公司计划对接支付宝会员卡功能,而任务恰巧由领导安排给我这边,小弟之前也未做过支付宝接口,研究了三天,终于将支付宝会员卡API接口大体上调通了,现将其整理下,以供参考. 蚂蚁金服开发平台-商户会员卡 ...

  8. keepalived实现服务高可用

    第1章 keepalived服务说明 1.1 keepalived是什么? Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可 ...

  9. google开源服务器apprtc的搭建

    本文参考网帖: http://www.jianshu.com/p/c55ecf5a3fcf http://io.diveinedu.com/2015/02/05/%E7%AC%AC%E5%85%AD% ...

  10. webrtc视频数据接收端处理流程详解