class Solution {
public int maxProfit(int[] prices) {
int result = 0;
for (int i = 1; i < prices.length; i++) {
result += Math.max(prices[i] - prices[i - 1], 0);
}
return result;
}
}
class Solution {
public boolean canJump(int[] nums) {
if(nums.length == 1){
return true;
}
int range = 0;
for(int i = 0; i <= range;i++){
range = Math.max(range, i + nums[i]);
if(range >= nums.length - 1){
return true;
}
}
return false;
}
}
class Solution {
public int jump(int[] nums) {
if(nums.length == 1){
return 0;
}
int curDistance = 0; // 当前覆盖最远距离下标
int ans = 0; // 记录走的最大步数
int nextDistance = 0; // 下一步覆盖最远距离下标
for(int i = 0; i < nums.length; i ++){
nextDistance = Math.max(nextDistance, i + nums[i]);
if(i == curDistance){
if(curDistance != nums.length - 1){
ans++;
curDistance = nextDistance;
if(nextDistance >= nums.length - 1){
break;
}
}else {
break;
}
}
}
return ans;
}
}