解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富?
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的更多相关文章
- leetcode day6 -- String to Integer (atoi) && 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 ...
- [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 ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- 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 ...
- 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 ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- php 的开发工具
通过上篇我们已经配置好了php的开发环境,我们就可以在这个模拟的环境下运行我们编写的php代码了. 在编写代码前,先安装一个自己喜欢的代码编辑器. 1.sublime text Sublime Tex ...
- 【Java入门提高篇】Day5 Java中的回调(二)
Java中有很多个Timer,常用的有两个Timer类,一个java.util包下的Timer,一个是javax.swing包下的Timer,两个Timer类都有用到回调机制.可以使用它在到达指定时间 ...
- Python简单爬虫
爬虫简介 自动抓取互联网信息的程序 从一个词条的URL访问到所有相关词条的URL,并提取出有价值的数据 价值:互联网的数据为我所用 简单爬虫架构 实现爬虫,需要从以下几个方面考虑 爬虫调度端:启动爬虫 ...
- node入门笔记
看了<node入门>http://www.nodebeginner.org/index-zh-cn.html.有些疑难点记下来. 在导出模块的时候给出的代码是这样的 var http = ...
- 迭代子模式(Iterator)
迭代子模式(Iterator) 顾名思义,迭代器模式就是顺序访问聚集中的对象,一般来说,集合中非常常见,如果对集合类比较熟悉的话,理解本模式会十分轻松.这句话包含两层意思:一是需要遍历的对象,即聚集对 ...
- dijkstra最小花费
//Gang #include<iostream> #include<cstring> #include<algorithm> #include<cstdio ...
- JavaWeb框架_Struts2_(七)----->文件的上传和下载
这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 1. 章节目录 Struts2文件上传 单文件上传 拦截器实现文件过滤 文件上传常量 ...
- MySQL 性能优化的最佳20多条经验分享(二)(转)
11. 尽可能的使用 NOT NULL 除非你有一个很特别的原因去使用 NULL 值,你应该总是让你的字段保持 NOT NULL.这看起来好像有点争议,请往下看. 首先,问问你自己"Empt ...
- 如何配置 Health Check?- 每天5分钟玩转 Docker 容器技术(107)
容器状态是 UP 的,应用就是健康的吗? 还真不一定!Docker 只能从容器启动进程的返回代码判断其状态,而对于容器内部应用的运行情况基本没有了解. 执行 docker run 命令时,通常会根据 ...
- CAN通讯的总结
1.CAN通讯有套国际标准,套协议版本号,种故障状态,种数据帧类型,种总线错误类型. 2.CAN的国际标准有两种ISO11898和ISO11519. 3.CAN2.0协议分为A版和B版两种,A版协议仅 ...