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

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. SaltStack 安装介绍 01

    一.入门指南 1.1 SALTSTACK是什么? The backbone of Salt is the remote execution engine, which creates a high-s ...

  2. Linux常用基础命令

    一.系统目录结构 约定俗成:   bin (binaries)存放二进制可执行文件   sbin (super user binaries)存放二进制可执行文件,只有root才能访问   etc (e ...

  3. 一步步搭建Retrofit+RxJava+MVP网络请求框架(二),个人认为这次封装比较强大了

    在前面已经初步封装了一个MVP的网络请求框架,那只是个雏形,还有很多功能不完善,现在进一步进行封装.添加了网络请求时的等待框,retrofit中添加了日志打印拦截器,添加了token拦截器,并且对Da ...

  4. zzuli 1816: 矩形 排序思维

    1816: 矩形 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 179  Solved: 54 SubmitStatusWeb Board Descr ...

  5. thinkphp5.0 微信公众号接入支付宝支付

    ---恢复内容开始--- 真是无力吐槽这个需求了,想骂客户,好端端的非要在微信公众号接入支付宝,都知道微信公众号是拒绝支付宝的,屏蔽了支付宝,所以在微信公众号接入支付宝的话就必须手动复制链接跳出微信内 ...

  6. 网页设计——2. html入门

    开始正式的课程讲解了,首先来看看课程体系: Java EE(java 企业应用程序版本) java2 有三个版本:J2 SE(标准版),J2 EE(企业版).J2 ME(微缩版). 我们要掌握J2EE ...

  7. 【Struts2的执行流程,这个博主写的很详细】

    http://blog.csdn.net/wjw0130/article/details/46371847

  8. 深入浅出了解OCR识别票据原理

    欢迎大家前往云加社区,获取更多腾讯海量技术实践干货哦~ 译者:Mr.Geek 本文翻译自dzone 中Ivan Ozhiganov所发文章Deep Dive Into OCR for Receipt ...

  9. 》》QQ-注册

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. oracle用户被锁定

    sqlplus sys/password@localhost:1521/cmsx as sysdba SQL*Plus: Release 11.2.0.1.0 Production on 星期一 7月 ...