文章链接:代码随想录
题目链接:122.买卖股票的最佳时机 II
class Solution {
public:
int maxProfit(vector<int>& prices) {
int result = 0;
for (int i = 1; i < prices.size(); i++){
result += max(prices[i] - prices[i - 1], 0);
}
return result;
}
};
class Solution {
public:
bool canJump(vector<int>& nums) {
int cover = 0;
for (int i = 0; i <= cover; i++){
cover = max(i + nums[i], cover);
if (cover >= nums.size() - 1) return true;
}
return false;
}
};
文章链接:代码随想录
题目链接:45.跳跃游戏 II
class Solution {
public:
int jump(vector<int>& nums) {
int cur = 0;
int next = 0;
int res = 0;
if (nums.size() == 1) return res;
for (int i = 0; i < nums.size(); i++){
next = max(i + nums[i], next);
if (i == cur){
res++;
cur = next;
if (next >= nums.size() - 1) break;
}
}
return res;
}
};
补卡。加油!!!