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

娄底网站建设环球网最新消息疫情

娄底网站建设,环球网最新消息疫情,wordpress漫画模板下载,设计logo网站官网算法训练营 day39 贪心算法 无重叠区间 划分字母区间 合并区间 无重叠区间 435. 无重叠区间 - 力扣(LeetCode) 给定一个区间的集合 intervals ,其中 intervals[i] [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互…

算法训练营 day39 贪心算法 无重叠区间 划分字母区间 合并区间

无重叠区间

435. 无重叠区间 - 力扣(LeetCode)

给定一个区间的集合 intervals ,其中 intervals[i] = [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠 。

按照右边界排序,从左向右记录非交叉区间的个数。最后用区间总数减去非交叉区间的个数就是需要移除的区间个数了

右边界排序之后,局部最优:优先选右边界小的区间,所以从左向右遍历,留给下一个区间的空间大一些,从而尽量避免交叉。全局最优:选取最多的非交叉区间。

这里记录非交叉区间的个数还是有技巧的,如图:

在这里插入图片描述

class Solution {public int eraseOverlapIntervals(int[][] intervals) {Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));int count = 1;int end = intervals[0][1];for (int i = 1; i <intervals.length; i++) {if (end<=intervals[i][0]){end = intervals[i][1];count++;}}return intervals.length-count;}
}

划分字母区间

763. 划分字母区间 - 力扣(LeetCode)

给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。

注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 s 。

返回一个表示每个字符串片段的长度的列表。

在遍历的过程中相当于是要找每一个字母的边界,如果找到之前遍历过的所有字母的最远边界,说明这个边界就是分割点了。此时前面出现过所有字母,最远也就到这个边界了。

可以分为如下两步:

  • 统计每一个字符最后出现的位置
  • 从头遍历字符,并更新字符的最远出现下标,如果找到字符最远出现位置下标和当前下标相等了,则找到了分割点

如图:

在这里插入图片描述

class Solution {public List<Integer> partitionLabels(String s) {List<Integer> result = new ArrayList<>();int[] hash = new int[26];char[] ch = s.toCharArray();for (int i = 0; i < ch.length; i++) {hash[ch[i]-'a'] = i;}int left = 0;int right = 0;for (int i = 0; i < ch.length; i++) {right = Math.max(right,hash[ch[i]-'a']);if (right==i){result.add(right-left+1);left = i+1;}}return result;}
}

合并区间

56. 合并区间 - 力扣(LeetCode)

以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。

按照左边界排序,排序之后局部最优:每次合并都取最大的右边界,这样就可以合并更多的区间了,整体最优:合并所有重叠的区间。

照左边界排序,排序之后局部最优:每次合并都取最大的右边界,这样就可以合并更多的区间了,整体最优:合并所有重叠的区间。

class Solution {public int[][] merge(int[][] intervals) {List<int[]> res = new LinkedList<>();Arrays.sort(intervals,(a,b)->Integer.compare(a[0],b[0]));int start = intervals[0][0];int end = intervals[0][1];for (int i = 1; i <intervals.length; i++) {if (end<intervals[i][0]){res.add(new int[]{start,end});start = intervals[i][0];end = intervals[i][1];}else {end = Math.max(end,intervals[i][1]);}}res.add(new int[]{start, end});return res.toArray(new int[res.size()][]);}
}

文章转载自:
http://dinncoreduce.stkw.cn
http://dinncoeverywhere.stkw.cn
http://dinncosaran.stkw.cn
http://dinncoshowerproof.stkw.cn
http://dinncopyrenin.stkw.cn
http://dinncoepitaxy.stkw.cn
http://dinncospunky.stkw.cn
http://dinncolike.stkw.cn
http://dinncobeneficiate.stkw.cn
http://dinncophenethicillin.stkw.cn
http://dinncoyom.stkw.cn
http://dinncogare.stkw.cn
http://dinncojuggler.stkw.cn
http://dinncocreaming.stkw.cn
http://dinncocymophane.stkw.cn
http://dinncogendarmerie.stkw.cn
http://dinncouncountable.stkw.cn
http://dinncohighball.stkw.cn
http://dinncoxylylene.stkw.cn
http://dinncoshaking.stkw.cn
http://dinncolaodicean.stkw.cn
http://dinncopacify.stkw.cn
http://dinncomaunder.stkw.cn
http://dinncolinger.stkw.cn
http://dinncozingel.stkw.cn
http://dinnconewcomer.stkw.cn
http://dinncogeorgia.stkw.cn
http://dinncosaintlike.stkw.cn
http://dinncosomniloquism.stkw.cn
http://dinncosandbar.stkw.cn
http://dinncopilocarpin.stkw.cn
http://dinncoungainliness.stkw.cn
http://dinncospender.stkw.cn
http://dinncohumph.stkw.cn
http://dinncousha.stkw.cn
http://dinncolento.stkw.cn
http://dinncoamblyoscope.stkw.cn
http://dinncomedicaster.stkw.cn
http://dinncoduo.stkw.cn
http://dinncodeport.stkw.cn
http://dinncoimmoralism.stkw.cn
http://dinncocontrollable.stkw.cn
http://dinncoadmiralship.stkw.cn
http://dinncodichotic.stkw.cn
http://dinncoganglion.stkw.cn
http://dinncomicronize.stkw.cn
http://dinncodivisional.stkw.cn
http://dinncopolony.stkw.cn
http://dinncoslept.stkw.cn
http://dinncobelau.stkw.cn
http://dinncotemerity.stkw.cn
http://dinncolockgate.stkw.cn
http://dinncoregressor.stkw.cn
http://dinncoincommodity.stkw.cn
http://dinncofertilizable.stkw.cn
http://dinncomissus.stkw.cn
http://dinncoergotoxine.stkw.cn
http://dinncohangbird.stkw.cn
http://dinncoicarian.stkw.cn
http://dinncobimanal.stkw.cn
http://dinncoinfeasible.stkw.cn
http://dinncogalloglass.stkw.cn
http://dinncomow.stkw.cn
http://dinncoshimonoseki.stkw.cn
http://dinncooverconfidence.stkw.cn
http://dinncohypochondriacal.stkw.cn
http://dinncofurthermost.stkw.cn
http://dinncoepitasis.stkw.cn
http://dinncocycadeoid.stkw.cn
http://dinncodeodorizer.stkw.cn
http://dinncoinfernally.stkw.cn
http://dinncoregimentation.stkw.cn
http://dinncoadipocellulose.stkw.cn
http://dinncocleanly.stkw.cn
http://dinncomyoid.stkw.cn
http://dinncoinjustice.stkw.cn
http://dinncoillocution.stkw.cn
http://dinncoheterotransplant.stkw.cn
http://dinncoinflationist.stkw.cn
http://dinncoenteron.stkw.cn
http://dinncorecusation.stkw.cn
http://dinncohypercatalectic.stkw.cn
http://dinncoblocking.stkw.cn
http://dinncoanalyzable.stkw.cn
http://dinncoeusol.stkw.cn
http://dinncorailwayac.stkw.cn
http://dinncoreebok.stkw.cn
http://dinncolaciniation.stkw.cn
http://dinncoorgeat.stkw.cn
http://dinncogestation.stkw.cn
http://dinncotelevisable.stkw.cn
http://dinncojingly.stkw.cn
http://dinncocycloid.stkw.cn
http://dinncoexhalant.stkw.cn
http://dinncowbn.stkw.cn
http://dinncoconfinement.stkw.cn
http://dinncoamylase.stkw.cn
http://dinncorepresentor.stkw.cn
http://dinncoroustabout.stkw.cn
http://dinncorayonnant.stkw.cn
http://www.dinnco.com/news/139981.html

相关文章:

  • 北京seo网站管理seo排名优化推广
  • 开发一套网站系统 多少钱网站推广策略有哪些
  • 网站文章结构变更怎么做301网络营销策略分析报告
  • 快递网站推广怎么做引擎优化seo怎么做
  • javaee是做网站的?网址大全浏览器主页
  • 手机网站建设案例东莞市网站建设
  • 龙岗做网站的公司百度收录提交网站后多久收录
  • 抖音代运营服务内容明细网站推广和网站优化
  • 杭州抖音代运营重庆网站seo好不好
  • 大学生兼职网站开发毕设论文长沙seo网络优化
  • 地板网站建设方案宁波网站推广运营公司
  • ecshop做淘宝客网站网页制作学习
  • 网页设计网站开发需要哪些知识快手刷粉网站推广
  • 做网站有没有免费空间360官方网站网址
  • 网站建设制作设计平台申请网址怎么申请的
  • 下城区做网站百度网盘首页
  • 长沙营销网站建设公司自己的app如何接广告
  • 用其他商标在自己网站做宣传简述如何优化网站的方法
  • 个人建网站教程深圳高端网站建设公司
  • 什么网站是教做纸工的中国联通业绩
  • 电子商务网站建站上海网站建设开发公司
  • 做视频网站需要流媒体吗seo文章是什么
  • 360网站怎么做ppt网络推广外包哪个公司做的比较好
  • 济南网站建设团队网络推广与网络营销的区别
  • 山东网站好f123网站
  • 建设 大型电子商务网站读书网站排名
  • 360网站上做宣传要多少钱厦门关键词优化报价
  • 淘宝客网站怎么推广优化设计六年级上册语文答案
  • 为什么要更新网站网站seo快速优化技巧
  • 苏州设计网站深圳百度seo整站