class Solution {
public:
int maxProfit(vector<int>& prices)
{
int profit = 0;
for (int i = 1; i < prices.size(); i++)
{
if (prices[i] > prices[i - 1])
profit += prices[i] - prices[i - 1];
}
return profit;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices)
{
int len = prices.size();
if (len < 2) //不足两天肯定没法赚钱
return 0;
int i_have = - prices[0];//第 i 天持有股票,身上有多少钱 假如初始身上没钱
int i_empty = 0;//第 i 身上没有股票 身上有多少钱 假如初始身上没钱
for (int i = 1; i < len; i++)
{
i_have = max(i_have, i_empty - prices[i]); //第i天有股票 = max(前一天有股票,前一天空仓今天买股票)
i_empty = max(i_empty, i_have + prices[i]); //第i天空仓 = max(前一天空仓,前一天有股票今天卖股票)
}
return i_empty;
}
};