LEETCODE: Best Time to Buy and Sell Stock II
2015-01-01 15:59
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).
熟能生巧,还是这题太简单了呢!
class Solution {
public:
int maxProfit(vector<int> &prices) {
int profits = 0;
for(int ii = 1; ii < prices.size(); ii ++) {
if(prices[ii] > prices[ii - 1]) {
profits += prices[ii] - prices[ii - 1];
}
}
return profits;
}
};
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 profits = 0;
for(int ii = 1; ii < prices.size(); ii ++) {
if(prices[ii] > prices[ii - 1]) {
profits += prices[ii] - prices[ii - 1];
}
}
return profits;
}
};
相关文章推荐
- 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 II
- [LeetCode] Best Time to Buy and Sell Stock II Solution
- leetcode 61: Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [Leetcode 51] 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 I II III
- [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] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)