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

贪心+矩阵算法

贪心算法

贪心的本质是:选择每一阶段的局部最优,从而达到全局最优

做题的时候,只要想清楚 局部最优 是什么,如果推导出全局最优,其实就够了。

买卖股票的最佳实际

思路:如果第i天卖出股票,则最大利润为(该天的股价-前面天数中最小的股价),然后与已知的最大利润比较,如果大于则更新当前最大利润的值

class Solution {public int maxProfit(int[] prices) {// 初始化最大利润为0,最低价格为第一个价格int maxProfit = 0;int minPrice = 100000;// 遍历价格数组for (int price : prices) {// 更新最低价格minPrice = Math.min(minPrice, price);// 更新最大利润maxProfit = Math.max(maxProfit, price - minPrice);}return maxProfit;}
}

跳跃游戏

class Solution {public boolean canJump(int[] nums) {int maxReach = 0; // 记录能到达的最远索引int n = nums.length;for (int i = 0; i < n; i++) {// 如果当前位置 i 已经超出最大可达范围,则说明无法继续前进if (i > maxReach) {return false;}// 更新最大可达范围maxReach = Math.max(maxReach, i + nums[i]);// 如果最大可达范围已经超过或等于最后一个索引,则返回 trueif (maxReach >= n - 1) {return true;}}return false;}
}

跳跃游戏Ⅱ

class Solution {public int jump(int[] nums) {int maxReach = 0;int current = 0;int jumps = 0;if( nums.length == 1) return 0;for(int i=0;i<nums.length-1;i++){maxReach=Math.max(i+nums[i],maxReach);if(i == current){jumps++;current = maxReach;if(current >= nums.length-1){return jumps;}}}return 0;}
}

划分字母区间

class Solution {public List<Integer> partitionLabels(String S) {char[] s = S.toCharArray();int n = s.length;int[] last = new int[26];for (int i = 0; i < n; i++) {last[s[i] - 'a'] = i; // 每个字母最后出现的下标}List<Integer> ans = new ArrayList<>();int start = 0, end = 0;for (int i = 0; i < n; i++) {end = Math.max(end, last[s[i] - 'a']); // 更新当前区间右端点的最大值if (end == i) { // 当前区间合并完毕ans.add(end - start + 1); // 区间长度加入答案start = i + 1; // 下一个区间的左端点}}return ans;}
}

矩阵

数组中第K个最大元素

class Solution {public int findKthLargest(int[] nums, int k) {int[] buckets = new int[20001];int n = nums.length;for(int i =0;i<n;i++){buckets[nums[i]+10000]++;}for(int i = 20000;i>=0;i--){k = k - buckets[i];if(k <= 0){return i-10000;}}return 0;}
}

有效括号

class Solution {public boolean isValid(String s) {//特殊情况if(s.isEmpty()){return true;}//创建栈,字符类型Stack<Character> stack = new Stack<Character>();for(char c:s.toCharArray()){if(c == '('){stack.push(')');}else if(c == '{'){stack.push('}');}else if(c=='['){stack.push(']');}// 要先判断是否为空,再判断出栈else if(stack.empty() || c!=stack.pop()){return false;}}if(stack.empty()){return true;}return false;}
}

每日温度

class Solution {public int[] dailyTemperatures(int[] temperatures) {int n = temperatures.length;int[] result = new int[n];Stack<Integer> stack = new Stack<>(); // 单调递减栈,存索引for (int i = 0; i < n; i++) {// 如果当前温度比栈顶索引的温度高,则计算等待天数while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {int prevIndex = stack.pop();result[prevIndex] = i - prevIndex;}// 当前索引入栈stack.push(i);}return result;}
}

http://www.dtcms.com/a/319163.html

相关文章:

  • Oracle 关闭 impdp任务
  • 云原生安全挑战与治理策略:从架构思维到落地实践
  • 基于大数据的美食视频播放数据可视化系统 Python+Django+Vue.js
  • 解读 gpt-oss-120b 和 gpt-oss-20b开源模型
  • 仓库管理系统-20-前端之记录管理的联表查询
  • Android中视图测量、布局、绘制过程
  • 嵌入式 - 数据结构:二叉树
  • GitHub 上 Star 数量前 20 的开源 AI 项目
  • X4000 私有 5G 实验室入门套件
  • 90-基于Flask的中国博物馆数据可视化分析系统
  • MySQL的变量、控制流程和游标:
  • 智能升级新纪元:基于Deepoc具身模型外拓开发板的除草机器人认知进化
  • git工程多个remote 拉取推送
  • 配置VScode内置Emmet自动补全代码
  • leetcode 415.字符串相加
  • 如何重塑企业服务体验?
  • 六边形架构模式深度解析
  • 深度学习(1):pytorch
  • SurgRIPE 挑战赛:手术机器人器械位姿估计基准测试|文献速递-医学影像算法文献分享
  • Next.js 样式:CSS 模块、Sass 等
  • 前端技术架构设计文档(Vue2+Antd+Sass)
  • 安全合规2--网络安全等级保护2.0介绍
  • A Logical Calculus of the Ideas Immanent in Nervous Activity(神经网络早期的M-P模型)
  • Spring Boot整合PyTorch Pruning工具链,模型瘦身手术
  • 记录一次Inspur服务器raid配置流程
  • 【数据库】如何从本地电脑连接服务器上的MySQL数据库?
  • 某梆企业壳frida检测绕过
  • 网页前端CSS实现表格3行平均分配高度,或者用div Flexbox布局
  • Springboot2+vue2+uniapp 实现搜索联想自动补全功能
  • vue2.如何给一个页面设置动态的name。不同路由使用一样的组件。页面不刷新怎么办?