Best Time to Buy and Sell Stock I && II
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
找到最大顺序差值
int maxProfit(int* prices, int pricesSize) {
int min = 0;
int max = 0;
int result_max = 0;
// go over array
if(pricesSize == 0)
return 0;
min = max = prices[0];
for(int i = 1; i < pricesSize; i++){
// find the first min
if(min > prices[i]){
min = prices[i];
max = prices[i];
}
// then find the first max
if(max < prices[i]){
max = prices[i];
}
// get the max result
if(result_max < max - min)
result_max = max - min;
}
return result_max;
}
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
得到可以赚到的最大值
int maxProfit(int* prices, int pricesSize) {
int min = 0;
int max = 0;
int sum = 0;
if(pricesSize == 0)
return 0;
min = max = prices[0];
// go over the array
for(int i = 1; i < pricesSize; i++){
// pass the prices less than front price
if(min > prices[i]){
min = prices[i];
max = prices[i];
}
// if we will have owner sell it and buy it again
if(max < prices[i]){
max = prices[i];
sum += max - min;
min = prices[i];
}
}
return sum;
}
- 这里min和max的名字不太符合,追赶法更合适
- 如果后面的值比前面的大就把差值加到sum上,然后重置min
Best Time to Buy and Sell Stock I && II的更多相关文章
- [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
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- 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 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 ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- 解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...
- 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
Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
随机推荐
- android第一天错误
1.自己写的activity并在AndroidManifest.xml中注册 在模拟机上运行时出现 No Launcher activity found! The launch will only s ...
- Android的Activity屏幕切换滑动动画
Activity的切换效果使用的是Android的动画效果,Android的动画在官方有相关资料:http://developer.android.com/guide/topics/graphics/ ...
- 2014年1月24日 Oracle 连接查询与子查询
1.乘积连接: 源表.源数据交叉链接,结果集数量为源数据之间的乘积 2.相等链接: 通过where关联几个数据源中的某一字段进行链接 3.自链接 自己链接自己 SSF A a1, A a2 ...
- canvas入门
<html> <head> <script> window.onload=function(){ var canvas=document.getElementByI ...
- GYP构建系统总结
GYP,Generate Your Project,一个Google开源的构建系统,最开始用于Chromium项目,现在一些其他的开源项目也开始使用GYP,如v8和node-gyp.不管怎样,这仅仅是 ...
- mysql查询优化技巧
索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里记录的优化技巧更适合开发人员,都是从网络上搜集和整理的,主要是查询语句上的优化,其他层面上 ...
- 【转】mysql行列转换方法总结
转:http://blog.chinaunix.net/uid-7692530-id-2567582.html 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解 ...
- nodejs学习随机记录
1. nodejs的顶层对象:global(nodejs中没有window) 2. nodejs一个文件就是一个模块 每个模块都有自己的作用域 通过var声明的变量,只属于当前模块下,并非全局的 va ...
- Python zxing 库解析(条形码二维码识别)
各种扫码软件 最近要做个二维码识别的项目,查到二维码识别有好多开源的不开源的软件 http://www.oschina.net/project/tag/238/ Zbar 首先试了一下Zbar,pyt ...
- More is better--hdu1856(并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...