class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = 0;
for(int i = 1; i < prices.size(); i++) {
if(prices[i] - prices[i - 1] > 0) {//只统计正利润
res += prices[i] - prices[i - 1];
}
// res += max(prices[i] - prices[i - 1], 0);//也可以这样写
}
return res;
}
};
class Solution {
public:
bool canJump(vector<int>& nums) {
int cover = 0;
if(nums.size() == 1) return true;
for(int i = 0; i <= cover; i++) {
cover = max(i + nums[i], cover);
if(cover >= nums.size() - 1) return true;
}
return false;
}
};
代码随想录:最买卖股票的佳时机II? 跳跃游戏?