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 ...
随机推荐
- Android之View绘制流程开胃菜---setContentView(...)详细分析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 1 为什么要分析setContentView方法 作为安卓开发者相信大部分都有意或者无意看过如下图示:PhoneWindow,DecorView这些 ...
- 如何写好git commit message
1.触发事件 我有这样一个版本库,里面包含两个学习用的练习项目:BookStore(以下简称BS)和PictureFriend(以下简称PF) 我在更改PF以后,未进行提交,同时又到BS中优化了一下文 ...
- Linux 环境下 MySQ导入和导出MySQL的sql文件
将服务器上的文件导入或导出还需要使用工具传输到本机中,推荐使用winscp,与xshell搭配使用 1 导入数据库 两种方法 .首先建空数据库 mysql>create database abc ...
- 第一次安装jshint,jshint新手使用记录
刚刚出来工作的渣渣,第一次进入这样比较正规的公司,各个开发流程都比较严格,代码也是要经过jshint的检测才能上传到svn才能成功打包项目.所以我这种技术都半桶水的职场开发小白,也是第一次用jshin ...
- jmeter测试教程
http://www.cnblogs.com/TankXiao/p/4045439.html
- java集合系列——List集合总结(六)
一.总结概述 List继承了Collection,是有序的列表. 实现类有ArrayList.LinkedList.Vector.Stack等 ArrayList是基于数组实现的,是一个数组队列.可以 ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
- 石子合并(NOI1995)
石子合并(NOI1995) 时间限制: 1 Sec 内存限制: 128 MB提交: 90 解决: 48[提交][状态][讨论版] 题目描述 在操场上沿一直线排列着 n堆石子.现要将石子有次序地合并 ...
- IDL 数组相关函数
1.信息获取 Size()函数能够获取数组的相关信息.不设置关键字是,函数返回变量的基本信息:第一个返回值是变量的维数N_dims,但当变量是敞亮或未定义是返回值为0:第二个 到第N_dims+1个返 ...
- mac idea sbt工程打jar包
1.首先保证sbt已下载,否则下载homebrew:在命令行输入/usr/bin/ruby XXX ->下载完成后在终端输入brew install sbt ->安装完毕后可以打jar包 ...