当前位置: 首页 > news >正文

贪心算法【Lecode_HOT100】

文章目录

        • 1.买卖股票的最佳时机No.121
        • 2.跳跃游戏No.55
        • 3.跳跃游戏IINo.45
        • 4.划分字母区间No.763

1.买卖股票的最佳时机No.121

在这里插入图片描述

class Solution {public int maxProfit(int[] prices) {if (prices == null || prices.length == 0) {return 0;}// 初始化买入价格为最大值,最大利润为 0int minPrice = Integer.MAX_VALUE;int maxProfit = 0;// 遍历价格数组for (int price : prices) {// 更新最低买入价格if (price < minPrice) {minPrice = price;}// 计算当前价格卖出的利润else {maxProfit = Math.max(maxProfit, price - minPrice);}}return maxProfit;}
}
2.跳跃游戏No.55

在这里插入图片描述

class Solution {public boolean canJump(int[] nums) {int maxReach = 0;  // 初始化为 0,表示起始位置能够到达的最远距离// 遍历数组for (int i = 0; i < nums.length; i++) {// 如果当前位置不可达,返回 falseif (i > maxReach) {return false;}// 更新能够到达的最远位置maxReach = Math.max(maxReach, i + nums[i]);// 如果能够到达最后一个位置,返回 trueif (maxReach >= nums.length - 1) {return true;}}return false;  // 遍历结束,若不能到达最后一个位置,则返回 false}
}
public boolean canJump(int[] nums) {if(nums.length==1) return true;int cover = 0;for(int i = 0;i<=cover;i++){cover = Math.max(cover,i+nums[i]);if(cover>=nums.length-1){return true;}}return false;}
3.跳跃游戏IINo.45

在这里插入图片描述

class Solution {public int jump(int[] nums) {int n = nums.length;// 如果数组长度为 1,不需要跳跃if (n == 1) return 0;int jumps = 0;int farthest = 0;int currentEnd = 0;// 遍历到倒数第二个元素即可for (int i = 0; i < n ; i++) {// 更新能到达的最远位置farthest = Math.max(farthest, i + nums[i]);// 如果到达了当前区间的末尾,进行跳跃if (i == currentEnd) {jumps++;currentEnd = farthest;// 如果当前区间的末尾已经能够到达最后一个位置,提前结束if (currentEnd >= n - 1) {break;}}}return jumps;}
}
public int jump(int[] nums) {if(nums.length==1) return 0;int curr = 0;int next = 0;int jump = 0;for(int i = 0;i<nums.length;i++){next = Math.max(next,i+nums[i]);if(i==curr){jump++;curr = next;if(curr>=nums.length-1) break;}}return jump;}
4.划分字母区间No.763

在这里插入图片描述

  • 思路
    • 统计每一个字符最后出现的位置
    • 从头遍历字符,并更新字符的最远出现下标,如果找到字符最远出现位置下标和当前下标相等了,则找到了分割点
class Solution {public List<Integer> partitionLabels(String s) {// Step 1: 记录每个字符最后出现的位置int[] lastIndex = new int[26];  // 因为字符是小写字母,共 26 个for (int i = 0; i < s.length(); i++) {lastIndex[s.charAt(i) - 'a'] = i;  // 更新字符的最后出现位置}List<Integer> result = new ArrayList<>();int currentEnd = 0;  // 当前片段的结束位置int start = 0;  // 当前片段的起始位置// Step 2: 遍历字符串,划分片段for (int i = 0; i < s.length(); i++) {currentEnd = Math.max(currentEnd, lastIndex[s.charAt(i) - 'a']);  // 更新当前片段的结束位置if (i == currentEnd) {  // 如果当前索引达到了当前片段的结束位置result.add(i - start + 1);  // 记录当前片段的长度start = i + 1;  // 更新下一个片段的起始位置}}return result;}
}
http://www.lryc.cn/news/507994.html

相关文章:

  • cmd初使用windows-docker时的一些小小问题
  • 使用qemu搭建armv7嵌入式开发环境
  • 火山引擎FORCE:智算能力全面升级
  • ARM 处理器平台 Ethernet Compliance 测试流程示例
  • 基于HAL库的stm32的can收发实验
  • 重构(二)
  • centos7下制作DockerFile 镜像
  • GFPS扩展技术原理(七)-音频切换消息流
  • 压缩qcow2镜像带来的性能损失简单分析
  • Kali操作系统简单介绍
  • LabVIEW物联网开发实战:专栏总述
  • 高效处理PDF文件的终极工具:构建一个多功能PDF转换器
  • Y3编辑器教程6:触发器进阶案例
  • react Ant Design
  • 汽车电子零部件(14):APA(自动泊车辅助)/RPA(远程遥控泊车)/AVP(自动代客泊车)
  • Hot100刷题计划-Day2-滑动窗口、双指针、数组、链表、动态规划
  • [react 3种方法] 获取ant组件ref用ts如何定义?
  • 考前倒计时98天
  • iterm2 focus时灰色蒙层出现的解决办法
  • 合并K个升序链表(最优解)
  • kubernates实战
  • How to run Flutter on an Embedded Device
  • airflow docker 安装
  • 浅析InnoDB引擎架构(已完结)
  • 华为云计算HCIE笔记02
  • 鸿蒙项目云捐助第十讲鸿蒙App应用分类页面二级联动功能实现
  • STM32低功耗模式结合看门狗
  • 数据迁移工具,用这8种!
  • Sapro编程软件
  • Python图注意力神经网络GAT与蛋白质相互作用数据模型构建、可视化及熵直方图分析...