代码随想录算法训练营第三十二天| 122.买卖股票的最佳时机II、55. 跳跃游戏、 45.跳跃游戏II

122.买卖股票的最佳时机II

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

解题思路:只算正利润

java:

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;
    }
}

55. 跳跃游戏

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

解题思路:每次取最大跳跃范围

java:

class Solution {
    public boolean canJump(int[] nums) {
        if (nums.length == 1) {
            return true;
        }
        int coverRange = 0;
        for (int i = 0; i <= coverRange; i++) {
            coverRange = Math.max(coverRange, i + nums[i]);
            if (coverRange >= nums.length - 1) {
                return true;
            }
        }
        return false;
    }
}

45.跳跃游戏II

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

解题思路:走到最大覆盖区域,count计数一次

java:

class Solution {
    public int jump(int[] nums) {
        if (nums == null || nums.length == 0 || nums.length == 1) {
            return 0;
        }
        int count=0;
        int curDistance = 0;
        int maxDistance = 0;
        for (int i = 0; i < nums.length; i++) {
            maxDistance = Math.max(maxDistance,i+nums[i]);
            if (maxDistance>=nums.length-1){
                count++;
                break;
            }
            if (i==curDistance){
                curDistance = maxDistance;
                count++;
            }
        }
        return count;
    }
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 02:56:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 02:56:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 02:56:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 02:56:04       20 阅读

热门阅读

  1. 特殊类的设计

    2024-01-28 02:56:04       32 阅读
  2. Canvas图像与视频基础,离屏19

    2024-01-28 02:56:04       31 阅读
  3. KY115 后缀子串排序

    2024-01-28 02:56:04       29 阅读
  4. 【Vue】1-3、Webpack 中的 loader

    2024-01-28 02:56:04       30 阅读
  5. 技术周总结 2024.01.22-01.28

    2024-01-28 02:56:04       39 阅读
  6. 蓝桥杯省一题单

    2024-01-28 02:56:04       35 阅读
  7. python的深浅拷贝

    2024-01-28 02:56:04       34 阅读
  8. HTTP协议(简单知识点)

    2024-01-28 02:56:04       40 阅读