1 贪心算法
代码语言:javascript复制class Solution {
public:
int maxProfit(vector<int>& prices) {
int curDiff = 0;
int money = 0;
for (int i = 0; i < prices.size(); i ) {
if (i == 0) continue;
curDiff = prices[i] - prices[i-1];
if (curDiff > 0)
money = curDiff;
}
return money;
}
};
致谢
图片来源于「代码随想录」公众号,欢迎大家关注这位大佬的公号