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的更多相关文章

  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. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

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

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

  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. 解题思路:best time to buy and sell stock i && ii && iii

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

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

  9. 【数组】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 ...

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

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

随机推荐

  1. Test 2.14

    i am back 写博客是个好习惯啊,要坚持下去才行 这些天的日子实在堕落

  2. asp.net 常用的3中身份验证

    1. windows验证: IIS根据应用程序的设置来进行身份验证,要使用这中验证方式,必须禁止使用匿名用户登录. 2. Forms验证: 通过Cookies来保存用户凭证,对未登录的用户 重定向到自 ...

  3. F, A, MS, QM, RF的OFFER和经历 -- Final update

    昨天收到FB的电话,我的OFFER已经批下来了,这也意味着我的JOB HUNTING结束了,下 面是我这两个月来申请结果汇总: Applications (7): Facebook, Google, ...

  4. UIView的交换实现,子视图交替变换

    其中加了一些动画  2016-01-13 其中主要的方法有:Demo下载地址,Demo中有介绍:https://github.com/lizhaojie001/UIview.git

  5. Java的动态代理机制详解(转)

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  6. 【搜索引擎Jediael开发4】V0.01完整代码

    截止目前,已完成如下功能: 1.指定某个地址,使用HttpClient下载该网页至本地文件 2.使用HtmlParser解释第1步下载的网页,抽取其中包含的链接信息 3.下载第2步的所有链接指向的网页 ...

  7. js 函数(function)

    <Javascript高级程序设计第三版> 3.7 函数 1. ECMAScript中的函数在定义时,不必指定是否返回值. 2. 位于return语句之后的任何code都永远不会执行.(之 ...

  8. JQ兼容各种JS库的写法

    来自为知笔记(Wiz)

  9. python 的内建函数

    lambda 函数:lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值 1. map/reduce 函数 (1)map()函数接收两个参数,一个是函数,一个是序列,map将传入 ...

  10. 禁止Chrome浏览器缓存的方法

    web开发的人经常chrome和firefox作为开发调试工具,有些时候需要禁止chrome浏览器缓存,最近也用到禁止缓存,以下介绍几种禁止chrome浏览器缓存的方法作为记录. HTML: < ...