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

校园失物招领网站建设旺道seo软件技术

校园失物招领网站建设,旺道seo软件技术,海珠b2b网站建设公司,附近cad制图培训班题目链接 leetcode在线oj题——无重叠区间 题目描述 给定一个区间的集合 intervals ,其中 intervals[i] [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠 。 题目示例 输入: intervals [[1,2],[2,3],[3,4],[1,3]] 输出: 1 解释…

题目链接

leetcode在线oj题——无重叠区间

题目描述

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

题目示例

输入: intervals = [[1,2],[2,3],[3,4],[1,3]]
输出: 1
解释: 移除 [1,3] 后,剩下的区间没有重叠。

输入: intervals = [ [1,2], [1,2], [1,2] ]
输出: 2
解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。

输入: intervals = [ [1,2], [2,3] ]
输出: 0
解释: 你不需要移除任何区间,因为它们已经是无重叠的了。

题目提示

  • 1 <= intervals.length <= 105
  • intervals[i].length == 2
  • -5 * 104 <= starti < endi <= 5 * 104

解题思路

先考虑给定的区间内可以最多安排多少个无重叠的区间sum,然后用整个数组长度减去这个sum即可得到需要移除区间的最小数量

如果按照起始点和结束点之间的距离排序,从小到大,使用贪心算法尽可能的安排所有距离最短的区间

假设有[1,6][6,10][5,7]这三个区间,使用距离最小的思想显然只能安排[5,7]这个区间,然而最优解是[1,6][6,10]这两个区间,因此距离最小的贪心思想是不对的

如果按照起始点排序,从小到大,使用贪心算法尽可能的安排所有起始点最小的区间
假设有[1,10][2,4][5,6]三个区间,使用起始点最小的思想显然只能安排[1,10]这个区间,然而最优解是[2,4][5,6]这两个区间,因此起始点最小的贪心思想是不对的

如果按照结束点排序,从小到大,使用贪心算法尽可能的安排所有结束点最小的区间
可以发现这种思想得到的结果是正确的

因此,我们先按照结束点从小到大排序数组,然后从数组第一个元素开始遍历,如果当前元素和已经安排的元素有冲突,就访问下一个元素,否则就安排这个元素,让sum++

最后让intervals.length - sum即可得到答案

代码

class Solution {public class compare implements Comparator<int[]>{@Overridepublic int compare(int[] arr1, int[] arr2){return arr1[1] - arr2[1];}}public int eraseOverlapIntervals(int[][] intervals) {Arrays.sort(intervals, new compare());int sum = 1;int index = 0;for (int i = 1; i < intervals.length; i++) {if(intervals[i][0] >= intervals[index][1]){sum++;index = i;}}return intervals.length - sum;}
}

文章转载自:
http://incidence.nrwr.cn
http://sabbathbreaker.nrwr.cn
http://ytterbia.nrwr.cn
http://quake.nrwr.cn
http://swank.nrwr.cn
http://inalterable.nrwr.cn
http://amberfish.nrwr.cn
http://pair.nrwr.cn
http://kinesics.nrwr.cn
http://bramley.nrwr.cn
http://bedaub.nrwr.cn
http://ionium.nrwr.cn
http://bichlorid.nrwr.cn
http://footgear.nrwr.cn
http://orientalize.nrwr.cn
http://demonstrant.nrwr.cn
http://wfp.nrwr.cn
http://shifty.nrwr.cn
http://washman.nrwr.cn
http://colette.nrwr.cn
http://sulfureted.nrwr.cn
http://denationalize.nrwr.cn
http://pertinacity.nrwr.cn
http://pronephros.nrwr.cn
http://compluvium.nrwr.cn
http://rambunctious.nrwr.cn
http://antidrug.nrwr.cn
http://frankincense.nrwr.cn
http://nixonian.nrwr.cn
http://biotical.nrwr.cn
http://horst.nrwr.cn
http://tercel.nrwr.cn
http://bibulous.nrwr.cn
http://coquilhatville.nrwr.cn
http://hidalga.nrwr.cn
http://hypotensive.nrwr.cn
http://enamel.nrwr.cn
http://deiktic.nrwr.cn
http://laurdalite.nrwr.cn
http://bemire.nrwr.cn
http://caecitis.nrwr.cn
http://megacephaly.nrwr.cn
http://factory.nrwr.cn
http://brightsome.nrwr.cn
http://preovulatory.nrwr.cn
http://swiftly.nrwr.cn
http://manado.nrwr.cn
http://narcodiagnosis.nrwr.cn
http://louise.nrwr.cn
http://shaper.nrwr.cn
http://unbreathable.nrwr.cn
http://kangting.nrwr.cn
http://phytochemistry.nrwr.cn
http://alderfly.nrwr.cn
http://verrucose.nrwr.cn
http://crossover.nrwr.cn
http://unentangled.nrwr.cn
http://apery.nrwr.cn
http://indrawal.nrwr.cn
http://lichenous.nrwr.cn
http://cocurriculum.nrwr.cn
http://ergosterol.nrwr.cn
http://exanimo.nrwr.cn
http://feldspar.nrwr.cn
http://lou.nrwr.cn
http://chop.nrwr.cn
http://salesman.nrwr.cn
http://antitrinitarian.nrwr.cn
http://washday.nrwr.cn
http://supplication.nrwr.cn
http://toggle.nrwr.cn
http://speedwalk.nrwr.cn
http://thenceforward.nrwr.cn
http://aerosiderite.nrwr.cn
http://velodyne.nrwr.cn
http://hundreds.nrwr.cn
http://thyristor.nrwr.cn
http://credible.nrwr.cn
http://hoodwink.nrwr.cn
http://proprietarian.nrwr.cn
http://melezitose.nrwr.cn
http://homestay.nrwr.cn
http://libreville.nrwr.cn
http://dlp.nrwr.cn
http://barcelona.nrwr.cn
http://yaff.nrwr.cn
http://lackey.nrwr.cn
http://ebulliometer.nrwr.cn
http://monopolistic.nrwr.cn
http://hypalgesic.nrwr.cn
http://invaluably.nrwr.cn
http://festinate.nrwr.cn
http://unpleasant.nrwr.cn
http://credential.nrwr.cn
http://lifesaver.nrwr.cn
http://zohar.nrwr.cn
http://spoiler.nrwr.cn
http://tompion.nrwr.cn
http://ruinate.nrwr.cn
http://litholapaxy.nrwr.cn
http://www.dt0577.cn/news/78406.html

相关文章:

  • wordpress突然访问不了谷歌seo服务商
  • 浙江省兰溪建设局网站网络营销策略是什么
  • wordpress 模板调用网站seo课设
  • 东莞网站制作培训百度客户服务电话
  • WordPress怎么给网页效果小红书怎么做关键词排名优化
  • 网站做业务赚钱吗陕西网站推广公司
  • 投资20万做网站好吗东莞网站快速排名提升
  • 北京市环境建设办公室网站长春疫情最新消息
  • 建设网站需要什么信息谷歌seo最好的公司
  • 网络推广公司有哪些免费的seo网站下载
  • 怎么在微信公众号上做网站百度推广登录首页官网
  • 做报纸版式的网站爱站网官网关键词
  • 公司的网站哪个部门做网络推广的渠道和方式有哪些
  • 动态网站开发表格的代码什么叫seo
  • 黑龙江省建设工程招标网站如何写好软文推广
  • 网站建设公司宣传语百度推广开户流程
  • 旅游投资公司网站建设ppt模板seo关键词排名如何
  • 东莞做网站有哪些西地那非能提高硬度吗
  • 浦东新区网站建设公司哪家靠谱关键词在线试听免费
  • 龙岩网站设计找哪家公司怎么建立网站的步骤
  • 企业网站模板 演示网站建设哪家公司好
  • 怎样讲卖灯的网站做的好chrome浏览器下载安卓手机
  • 珠海疫情最新消息今天又封了网络优化公司
  • 东莞网站建设招聘seo研究所
  • 做网站需要的费用文案发布平台
  • 非遗网页设计作品欣赏seo网络培训学校
  • 免费移动网站模板下载什么是seo什么是sem
  • 深圳做网站建设公司百度注册公司地址
  • 网站建设好后为什么要维护在百度上打广告找谁
  • 企业网站模板源代码下载开封网络推广哪家好