121.买卖股票的最佳时机
public int maxProfit(int[] prices) {
int max=0;
int min=Integer.MAX_VALUE;
for(int i=0;i<prices.length;i++){
if(prices[i]<min){
min=prices[i];
}else{
max= Math.max(max,prices[i]-min);
}
}
return max;
}
55.跳跃游戏
private static boolean canJump(int[] nums) {
int n = nums.length;
int rightmost=0;
for(int i=0;i<n;i++){
if(i<=rightmost){
rightmost = Math.max(rightmost,nums[i]+i);
if(rightmost>=n-1){
return true;
}
}
}
return false;
}
45.跳跃游戏II
public int jump(int[] nums) {
int n=nums.length;
int step=0;
int position=n-1;
while(position>0){
for(int i=0;i<position;i++){
if(nums[i]+i>=position){
position=i;
step++;
break;
}
}
}
return step;
}