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 ...
随机推荐
- 框架应用:Mybatis (三) - 关系映射
你有一张你自己的学生证?(一对一) 你这一年级有多少个学生?(一对多) 班上学生各选了什么课?(多对多) 两张表以上的操作都需要联立多张表,而用SQL语句可以直接联立两张表,用工程语言则需要手动完成这 ...
- Hadoop的safeMode
当集群启动的时候,会首先进入到安全模式.系统在安全模式下,会检查数据块的完整性.假设我们设置的副本数(即参数dfs.replication)是5,那么在dataNode上就应该有5个副本存在,假设只存 ...
- [python学习笔记] python程序打包成exe文件
安装 pyinstaller pip3 install pyinstaller 命令 pyinstaller -F -w -i ../ui/icon.ico --clean ../Login.py 参 ...
- [python学习笔记] 数据类型与语法
数据类型 数值型 int 整形 没有long类型,可以代表任意大小的整数. type(1) -> int float 浮点数 也没有double类型 type(1.2) -> float ...
- Poj 1032 Parliament
Parliament Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19103 Accepted: 8101 Descr ...
- java集合系列——List集合之ArrayList介绍(二)
一:List概述 List是 java.util包下面的类,从<a href="http://blog.csdn.net/u010648555/article/details/5604 ...
- HDFS概述(1)————HDFS架构
概述 Hadoop分布式文件系统(HDFS)是一种分布式文件系统,用于在普通商用硬件上运行.它与现有的分布式文件系统有许多相似之处.然而,与其他分布式文件系统的区别很大.HDFS具有高度的容错能力,旨 ...
- Bear and Floodlight 状态压缩DP啊
Bear and Floodlight Time Limit: 4000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...
- Flip Game poj 1753
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29731 Accepted: 12886 Descr ...
- 石子合并(NOI1995)
石子合并(NOI1995) 时间限制: 1 Sec 内存限制: 128 MB提交: 90 解决: 48[提交][状态][讨论版] 题目描述 在操场上沿一直线排列着 n堆石子.现要将石子有次序地合并 ...