LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)
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).
题目标签:Array, Greedy
这次的题目可以允许我们多次买卖,而 #121题 只允许我们买卖一次。
回来更新一下解题思路,觉得之前的思路有一点繁琐,现在这个更简单明了。
如何找到最大收益呢,我们可以把数字想象成对应高度的柱状图,当一整段array里,怎么才是最大收益呢?
就是把所有上升区间里的 最大值(最后一个) 减去 最小值(第一个) 加在一起,就是最大收益值了。
当我们遇到一个下降的数字,那么此时就是需要把之前的上升区间的profit 值加入maxProfit里的时候了。
如果一直都是下降区间的话? 每次我们遇到一个下降数字,会把 前面一个数字end-1 减去 start 加入maxProfit,然后在更新start = end。
所以遇到下降数字的时候,其实就是利用 一个数字减去自己,把0 加入maxProfit,不会影响答案。
在遇到下降数字时候,start 和 end 不会分开;只有在上升区间里,start 和 end 才会分开,产生一个区间。
Java Solution:
Runtime beats 52.20%
完成日期:10/04/2017
关键词:Array, Greedy
关键点:找到所有的ascending range 把它们的profit 累加。
class Solution
{
public int maxProfit(int[] prices)
{
if(prices == null || prices.length == 0)
return 0; int maxProfit = 0;
int start = 0;
int end = 1; while(end < prices.length) // iterate prices array
{
if(prices[end-1] > prices[end]) // if find any descending number
{
maxProfit += (prices[end-1] - prices[start]); // add the previous ascending profit
start = end; // update start pointer from this descending number
} end++;
} // take care the last part
maxProfit += (prices[end-1] - prices[start]); return maxProfit;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)的更多相关文章
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
- LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- AIX盘rw_timeout值过小导致IO ERROR
刚下班没多久,接收到告警提示数据库的数据文件异常,且同时收到主机硬盘的IO ERROR告警 该数据库服务器为AIX+oracle 9i环境,登录主机验证关键日志告警 发现确实在18点48分有磁盘IO的 ...
- JavaSE(十)之Collection总结
前面几篇把集合中的知识大概都详细的说了一遍,但是我觉得还是要总结一下,这样的话,可以更好的理解集合. 一.Collection接口 首先我们要一张图来说明: Collection接口,它是集合的顶层接 ...
- Html语言,使用<a>标签发送电子邮件
<a>标签有一个功能是可以链接Email地址,使用mailto能让访问者便捷向网站管理者发送电子邮件. 使用mailto 属性时请参考下表: 如果mailto里同时有多个参数,第一个参数必 ...
- Linux入门之常用命令(2)
(三) 链接文件 ln [-s] [源文件] [目标文件] -s表示符号链接 没有则是硬链接 硬链接是一个独立文件 (相当于一个副本) 符号链接是一个链接文件(相当于一个快捷方式) 但是修 ...
- 洗礼灵魂,修炼python(3)--从一个简单的print代码揭露编码问题,运行原理和语法习惯
前期工作已经准备好后,可以打开IDE编辑器了,你可以选择python自带的IDLE,也可以选择第三方的,这里我使用pycharm--一个专门为python而生的IDE 按照惯例,第一个python代码 ...
- SLB vs CLB
什么是SLB? SLB, 服务器负载均衡(Server Load Balancing),可以看作HSRP(热备份路由器协议)的扩展,实现多个服务器之间的负载均衡. 虚拟服务器代表的是多个真实服务器的群 ...
- Angular JS的正确打开姿势——简单实用(上)
前 言 絮叨絮叨 在分享了JS和JQuery之后,今天再给大家分享一个好玩并且特别好用JS框架--AngularJS. 一. AngularJS简介 AngularJS 诞生于2009年,由 ...
- 屏蔽掉Google Chrome 浏览器 textarea 单词拼写检测
可以使用html5的spellcheck属性来关闭对元素内容进行拼写检查. <!-以下两种书写方法正确--> <textarea spellcheck="true" ...
- HDU1085 多重背包
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- Java web 入门知识 及HTTP协议详解
Java web 入门知识 及HTTP协议详解 WEB入门 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资 ...