LeetCode: Best Time to Buy and Sell Stock II
2014-03-03 14:43
323 查看
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).
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) { int maxProfit = 0; for(int i = 1; i < prices.size(); i++){ int diff = prices[i] - prices[i-1]; if(diff > 0){ maxProfit += diff; } } return maxProfit; } };
相关文章推荐
- leetcode : Best Time to Buy and Sell Stock II
- 【Leetcode】Best Time to Buy and Sell Stock II in JAVA
- 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 "Best Time to Buy and Sell Stock II"
- leetcode 刷题之路 72 Best Time to Buy and Sell Stock II
- LeetCode OJ Best Time to Buy and Sell Stock I II III IV
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-Easy刷题(30) 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][python]Best Time to Buy and Sell Stock I/II/III
- [leetcode][贪心] Best Time to Buy and Sell Stock II
- 122.leetcode Best Time to Buy and Sell Stock II(medium)[贪心算法 数组]
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode——Best Time to Buy and Sell Stock II