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. plsql导入一个目录下全部excel

    import java.io.File; import java.util.ArrayList; import jxl.Sheet; import jxl.Workbook; import com.j ...

  2. 正确理解HTML,XHTML页面的头部doctype定义

    摘自http://www.west263.com/info/html/wangyezhizuo/css/20080225/42390.html 当我们制作页面的时候,总会在它的源代码头部看到一串声明, ...

  3. Linux共享wifi给Android手机

    亲測可行,測试系统:Deepin2014,Ubuntu也一样.步骤很easy. 1.卸载hostapd,sudo apt-get remove hostapd(假设原来装过的话卸载,由于某些版本号不支 ...

  4. 解决windows下的mysql匿名登陆无法使用mysql数据库的问题

    原文:解决windows下的mysql匿名登陆无法使用mysql数据库的问题 我在windows下安装了mysql,但是不用密码就能登进去,而root明明是有密码的,我用select user()命令 ...

  5. android 在特殊应用的特殊功能,以帮助通信系统的问题

    在实际工程中的应用,进入一个特殊的应用后,系统的某个功能不能起作用. 当然,这个通信有非常多办法能够做到.笔者能够想到的至少有例如以下几种 1.利用property熟悉来实现,这种话须要添加一个特殊的 ...

  6. 使用SVN clang: error: linker command failed with exit code 1 (use -v to see invocation)

    然后上传到该项目SVN仓库上,例如,下面的错误再次发生再拉到本地编译 ld: library not found for -lxxxxxxxxxxxx clang: error: linker com ...

  7. 向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题

    原文:向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题 GridView的一个模板列中的内容是按钮,需要实现以下的效果: GridView分页显示数据,点击编辑按钮(模 ...

  8. CH BR4思考熊(恒等有理式-逆波兰表达式求值)

    恒等有理式 总时限 10s 内存限制 256MB 出题人 fotile96 提交情况 4/43 描述 给定两个有理式f(X)与g(X),判断他们是否恒等(任意A,如果f(A)与g(A)均有定义,那么f ...

  9. win7系统u盘安装过程

    1.准备好带有启动项的U盘,并把镜像解压到里面去 2.插上u盘,开机长按del键进入bois设置界面 在boot页面 1.boot device priority->1st boot devic ...

  10. eclipse3.1.1汉化版安装

    确认安装好jdk以后,下载eclipse3.1.1及多语言包eclipse3.1.1 下载地址   http://eclipse.areum.biz/downloads/drops/R-3.1.1-2 ...