Best Time to Buy and Sell Stock II DP买卖股票
2015-05-07 18:06
357 查看
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).
class Solution { public: //sum = sum+a[i+1]-a[i](if a[i+1]>a[i]) int maxProfit(vector<int>& prices) { if(prices.size()==0) return 0; int sum=0; for(int i=1;i<prices.size();i++) { if(prices[i]>prices[i-1]) sum+=prices[i]-prices[i-1]; } return sum; } };
相关文章推荐
- [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 (股票买卖时机问题2)
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- 122. Best Time to Buy and Sell Stock II 最佳买卖股票的时间
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- Best Time to Buy and Sell Stock III 最佳时间买入卖出股票(最多两次买卖)@LeetCode
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode
- LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- leetcode122.[DP] Best Time to Buy and Sell Stock II
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
- Best Time to Buy and Sell Stock 【最佳买卖股票的时间】【容易】
- LeetCode OJ:Best Time to Buy and Sell Stock(股票买卖的最佳时期)
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)