思路:假设每天都进行买卖,设temp为第i天与i - 1天的价格之差,若temp > 0,则加入maxProfit中
class Solution {
public:
int maxProfit(vector<int>& prices)
{
if(prices.size() == 0)
return 0;
int maxProfit, i, temp;
maxProfit = 0;
for(i = 1; i < prices.size() ; i++)
{
temp = prices[i] - prices[i - 1];
if(temp > 0)
{
maxProfit += temp;
}
}
return maxProfit;
}
};
public class Solution {
public int MaxProfit(int[] prices)
{
int i = 0, max = 0, current;
while(i < prices.Length - 1)
{
current = prices[i + 1] - prices[i];
if(current > 0)
{
max += current;
}
i++;
}
return max;
}
}