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).
Array Greedy
题目地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
解题思路:
也是关于股票买卖的最大收益问题。这里的要求是你可以尽可能多的进行买卖
这里解法就是只要有赚就买!
代码:
class Solution {
public:
int maxProfit(vector<int> &prices) {
//要求最大收益,并且可以有无限次的交易,那么只要有收益就进行交易,这样的收益肯定最大
if(prices.size() == 0) return 0;
int p = 0;
for(int i = 0;i < prices.size() - 1;i++){
if(prices[i + 1] > prices[i]){
p += prices[i + 1] - prices[i];
}
}
return p;
}
};