leetcode 435. Non-overlapping Intervals

0人阅读 评论(0) 收藏 举报
分类:

借鉴了别人的代码,写成java

package leetcode;

import java.util.Arrays;
import java.util.Comparator;

class Interval {
    int start;
    int end;
    Interval() { start = 0; end = 0; }
    Interval(int s, int e) { start = s; end = e; }
}

class myComparator implements Comparator<Interval> {
    public int compare(Interval a, Interval b) {
        return a.end - b.end;
    }
}


public class NonoverlappingIntervals {

    public static int eraseOverlapIntervals(Interval[] intervals) {
        if (intervals.length == 0)  return 0;
        Arrays.sort(intervals, new myComparator());
        float end = (float) (-1/0.0);
        int erased = 0;    
        for (Interval i:intervals){
            if (i.start >= end)
                end = i.end;
            else
                erased += 1;               
        }         
        return erased;
    }

    public static void main(String[] args) {

        Interval[] intervals = new Interval[3];
        Interval x = new Interval(1,3);
        Interval y = new Interval(3,5);
        Interval z = new Interval(5,6);
        Arrays.fill(intervals,0,1,x);
        Arrays.fill(intervals,1,2,y);
        Arrays.fill(intervals,2,3,z);
        System.out.println(eraseOverlapIntervals(intervals));
    }
}
查看评论

Leetcode 435. Non-overlapping Intervals 无交叉覆盖区间 解题报告

1 解题思想题目给了一堆区间[起始位置,结束位置] 这些区间可能会有一些交叠的地方(边界相邻不算),现在要求找到一个方法,可以做最少的删除动作,使得剩下的区间都不交叉覆盖,返回那个需要删除的最少次数...
  • MebiuW
  • MebiuW
  • 2016-11-06 14:48:21
  • 1969

LeetCode 435. Non-overlapping Intervals

Problem StatementGiven a collection of intervals, find the minimum number of intervals you need to r...
  • junchen1992
  • junchen1992
  • 2016-10-30 14:22:09
  • 656

435. Non-overlapping Intervals

435. Non-overlapping Intervals Given a collection of intervals, find the minimum number...
  • liuchenjane
  • liuchenjane
  • 2016-10-30 16:33:26
  • 1345

Non-overlapping Intervals

Given a collection of intervals, find the minimum number of intervals you need to remove to make the...
  • awawfwfw
  • awawfwfw
  • 2016-11-18 03:54:44
  • 416

leetcode 435. Non-overlapping Intervals (贪心)

题意: 给你几个区间,删除最少的区间个数,使得这几个区间不重叠。边界可以忽略。 思路: 有重叠的情况下,尽量使得区间分散一些。先把区间按左端点升序,如果相同,按右端点升序。从左端最小的开始放。如...
  • exp1ore
  • exp1ore
  • 2017-08-18 09:20:17
  • 133

Leetcode 435. Non-overlapping Intervals

题目Given a collection of intervals, find the minimum number of intervals you need to remove to make t...
  • sdcs2016_dyq
  • sdcs2016_dyq
  • 2017-01-16 09:51:35
  • 135

[LeetCode] 435. Non-overlapping Intervals

[LeetCode] 435. Non-overlapping Intervals
  • weixin_39629939
  • weixin_39629939
  • 2017-10-21 15:50:40
  • 46

[LeetCode]435. Non-overlapping Intervals

https://leetcode.com/problems/non-overlapping-intervals/?tab=Description 找出删除最少之后不覆盖的删除数 ...
  • gqk289
  • gqk289
  • 2017-02-18 20:58:46
  • 218

【LeetCode】 435. Non-overlapping Intervals

Given a collection of intervals, find the minimum number of intervals you need to remove to make the...
  • qq_31129455
  • qq_31129455
  • 2017-01-14 22:56:47
  • 78

【LeetCode】435. Non-overlapping Intervals

题目: Given a collection of intervals, find the minimum number of intervals you need to remove to...
  • gmr_vhp
  • gmr_vhp
  • 2017-05-19 15:47:09
  • 146
    个人资料
    等级:
    访问量: 369
    积分: 64
    排名: 161万+
    文章存档