LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
2014-10-09 21:40
465 查看
问题: 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).
这道题是Best Time to Buy and Sell Stock I问题的第二个版本,题目整体的要求没有变,和I一样,只是在这里不再只限一次交易,此题中可以多次交易,只是买之前必须先卖出去,且初始手里没有股票。
分析: 在这道题中由于买卖的次数是不做限制的,而且是不买卖股票是不收手续费的,所以只要第二天的价格比第一天低我们就可以买,比如:
代码如下(java):
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).
这道题是Best Time to Buy and Sell Stock I问题的第二个版本,题目整体的要求没有变,和I一样,只是在这里不再只限一次交易,此题中可以多次交易,只是买之前必须先卖出去,且初始手里没有股票。
分析: 在这道题中由于买卖的次数是不做限制的,而且是不买卖股票是不收手续费的,所以只要第二天的价格比第一天低我们就可以买,比如:
数组是[7,8,9,1,2,3,2,1]
在这个数组中我们可以7买入,9卖出,再1买入,3卖出,这就是我们能找到的最大收益了。
但是这和我提到的第二天比第一天低我们就买有什么关系呢?
7买9卖,我们可以这么看,
第一天,价格为7,第二天价格8>7,所以我们7买入,花了7;
第二天,价格为8,首先把第一天买的买了,然后再看9>8,然后又8买入,没赚;
第三天,价格为9,把第二天8买的买了,然后再看1<9,不买了,赚了2;
以此类推。。。
所以这道题说白了就是然你找到递增的子数列,然后所有递增子数列最大-最小的和就是最大收益。在这个数组中我们可以7买入,9卖出,再1买入,3卖出,这就是我们能找到的最大收益了。
但是这和我提到的第二天比第一天低我们就买有什么关系呢?
7买9卖,我们可以这么看,
第一天,价格为7,第二天价格8>7,所以我们7买入,花了7;
第二天,价格为8,首先把第一天买的买了,然后再看9>8,然后又8买入,没赚;
第三天,价格为9,把第二天8买的买了,然后再看1<9,不买了,赚了2;
以此类推。。。
代码如下(java):
public class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length == 0)return 0; int profit = 0; for(int i=1; i<prices.length;i++){ if(prices[i]>prices[i-1]){ profit += prices[i]-prices[i-1]; } } return profit; } }
相关文章推荐
- LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
- [LeetCode] Best Time to Buy and Sell Stock I II III IV 股票买卖
- LeetCode题库解答与分析——#121. 买卖股票的最佳时机BestTimeToBuyAndSellStock
- LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)
- [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机
- Leetcode121 Best Time to Buy and Sell Stock(买卖股票的最佳时机1)
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
- [Java语言] [Leetcode] Best Time to Buy and Sell Stock 买卖股票的最佳时机
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益
- LeetCode | Best Time to Buy and Sell Stock II(股票购买抛售问题II)
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- 【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II
- [LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)
- 【LintCode】 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III