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;
if (nums.size() == 1) return true;
for (int i = 0; i <= cover; i++) {
cover = max(cover, i + nums[i]);
if (cover >= nums.size() - 1) return true;
}
return false;
}
};
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() == 1) return 0;
int cur = 0;
int next = 0;
int res = 0;
for (int i = 0; i < nums.size(); i++) {
next = max(next, nums[i] + i);
if (i == cur) {
res++;
cur = next;
if (next >= nums.size() - 1) break;
}
}
return res;
}
};