[leetcode][贪心] Best Time to Buy and Sell Stock II
2015-05-21 21:51
459 查看
题目:
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).
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).
class Solution { public: //将所有的上升部分的差值加起来即可 int maxProfit(vector<int>& prices) { if(prices.size() <= 1) return 0; int res = 0; for(int i = 1 ; i < prices.size(); ++i){ if(prices[i] > prices[i-1]) res += prices[i] - prices[i-1]; } return res; } };
相关文章推荐
- leetcode---Best Time to Buy and Sell Stock II---贪心
- LeetCode 122 Best Time to Buy and Sell Stock II (贪心)
- LeetCode -- Best Time to Buy and Sell Stock II (贪心策略,差分序列)
- Leetcode 贪心 Best Time to Buy and Sell StockII
- leetcode-122-Best Time to Buy and Sell Stock II-贪心
- Leetcode 122 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
- Best Time to Buy and Sell Stock I,II,III [leetcode]
- LeetCode 之 Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- Java for LeetCode 122 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 122 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 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)