leetcode解题之122# Best Time to Buy and Sell Stock II
2017-03-13 21:54
465 查看
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).
本题由于是可以操作任意次数,只为获得最大收益,而且对于一个上升子序列,比如:[5, 1, 2, 3, 4]中的1, 2, 3, 4序列来说,对于两种操作方案:
1 在1买入,4卖出
2 在1买入,2卖出同时买入,3卖出同时买入,4卖出
这两种操作下,收益是一样的。
所以可以从头到尾扫描prices,如果price[i] – price[i-1]大于零则计入最后的收益中。即贪心法
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).
本题由于是可以操作任意次数,只为获得最大收益,而且对于一个上升子序列,比如:[5, 1, 2, 3, 4]中的1, 2, 3, 4序列来说,对于两种操作方案:
1 在1买入,4卖出
2 在1买入,2卖出同时买入,3卖出同时买入,4卖出
这两种操作下,收益是一样的。
所以可以从头到尾扫描prices,如果price[i] – price[i-1]大于零则计入最后的收益中。即贪心法
public int maxProfit(int[] prices) { 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 II 解题报告
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- leetCode Best Time to Buy and Sell Stock II解题分享
- LeetCode-Best Time to Buy and Sell Stock II-解题报告
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- [leetcode]Best Time to Buy and Sell Stock II
- leetcode--Best Time to Buy and Sell Stock II
- [Leetcode 51] 122 Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II Solution
- LeetCode_Best Time to Buy and Sell Stock II
- [leetcode刷题系列]Best Time to Buy and Sell Stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode]Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock I II III
- [Leetcode] Best Time to Buy and Sell Stock II