Best Time to Buy and Sell Stock I

你只能一个操作:维修preMin拍摄前最少发生值

代码例如以下:

    int maxProfit(vector<int> &prices) {
if (prices.size() == 0) return 0;
int profit = 0;
int preMin = prices[0];
for (int i = 1; i < prices.size(); i++)
{
if (prices[i] < preMin) preMin = prices[i];
profit = max(profit, prices[i] - preMin);
}
return profit;
}

Best Time to Buy and Sell Stock II

能无限次操作时:当==>当前价格 > 买入价格的时候,卖出

代码例如以下:

    int maxProfit(vector<int> &prices) {
if (prices.size() == 0) return 0;
int profit = 0;
int buy = prices[0];
for (int i = 1; i < prices.size(); i++)
{
if (buy < prices[i])
profit += prices[i] - buy;
buy = prices[i];
}
return profit;
}

Best Time to Buy and Sell Stock III

仅仅能操作两次时:

两次操作不能重叠。能够分成两部分:0...i的最大利润fst  和i...n-1的最大利润snd

代码例如以下:

    int maxProfit(vector<int> &prices) {
if (prices.size() == 0) return 0;
int size = prices.size();
vector<int> fst(size);
vector<int> snd(size); int preMin = prices[0];
for (int i = 1; i < size; i++)
{
if (preMin > prices[i]) preMin = prices[i];
fst[i] = max(fst[i - 1], prices[i] - preMin);
}
int profit = fst[size - 1];
int postMax = prices[size - 1];
for (int i = size - 2; i >= 0; i--)
{
if (postMax < prices[i]) postMax = prices[i];
snd[i] = max(snd[i + 1], postMax - prices[i]);
//update profit
profit = max(profit, snd[i] + fst[i]);
}
return profit;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Best Time to Buy and Sell Stock I,II,III [leetcode]的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

  6. 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 ...

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

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

  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. 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记

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

随机推荐

  1. swift 笔记2

    swift交流群:342581988,欢迎增加. 今天真郁闷啊,把mac升级到10.10了.如今好了,曾经的程序都跑不了了.哎,不说了,让我郁闷会再. 说说条件推断吧,事实上这些基本的语法大家都知道肯 ...

  2. 国内国外MD5在线解密站点

    -http://www.cmd5.com/english.aspx (457,354,352,282) - http://www.md5crack.com - http://www.hashcheck ...

  3. hyper-v 报错 0x80070569

    在Windows8.1Pro版使用过程中,突然出现HYPER-V无法创建虚拟机.显示错误为: 登录失败:未授予用户在此计算机上的请求登录类型.(0x80070569). 回顾起近期通过组策略增强了系统 ...

  4. Oracle数据库案例整理-Oracle系统执行时故障-Shared Pool内存不足导致数据库响应缓慢

    1.1       现象描写叙述 数据库节点响应缓慢,部分用户业务受到影响. 查看数据库告警日志,開始显示ORA-07445错误,然后是大量的ORA-04031错误和ORA-00600错误. 检查数据 ...

  5. 什么是gulp?

    gulp初涉 1.什么是gulp? gulp是前端开发过程中一种基于流的代码构建工具,是自动化项目的构建利器:它不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用 ...

  6. Codeforces 549G. Happy Line 馋

    非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...

  7. 完美攻略心得之圣魔大战3(Castle Fantisia)艾伦希亚战记(艾伦西亚战记)包含重做版(即新艾伦希亚战记)

    (城堡幻想曲3,纠正大家个错误哦,不是圣魔大战3,圣魔大战是城堡幻想曲2,圣魔大战不是个系列,艾伦西亚战记==艾伦希亚战记,一个游戏日文名:タイトル キャッスルファンタジア -エレンシア戦記-リニュー ...

  8. 静默安装MSSQL

    原文地址:http://www.cnblogs.com/lyhabc/p/3511788.html 介绍 假如你有50台服务器需要安装SQLSERVER,如果你用下一步下一步的方式,用远程桌面不停切换 ...

  9. Linux查看进程线程个数

    1.根据进程号进行查询: # pstree -p 进程号 # top -Hp 进程号 2.根据进程名字进行查询: # pstree -p `ps -e | grep server | awk '{pr ...

  10. Windows Phone开发(11):常用控件(下)

    原文:Windows Phone开发(11):常用控件(下) WP控件大部分都可以从Silverlight中继承过来,这里我也只能拿一部分作演示,对于其它控件如何使用,可以参考SDK相关说明以及Sil ...