Skip to content

Commit f069d44

Browse files
author
freemanzhang
committed
init impl
1 parent 8618831 commit f069d44

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package interval;
2+
3+
import java.util.Arrays;
4+
5+
import utility.Interval;
6+
7+
/*
8+
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
9+
10+
Note:
11+
You may assume the interval's end point is always bigger than its start point.
12+
Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
13+
Example 1:
14+
Input: [ [1,2], [2,3], [3,4], [1,3] ]
15+
16+
Output: 1
17+
18+
Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
19+
Example 2:
20+
Input: [ [1,2], [1,2], [1,2] ]
21+
22+
Output: 2
23+
24+
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
25+
Example 3:
26+
Input: [ [1,2], [2,3] ]
27+
28+
Output: 0
29+
30+
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
31+
Show Tags
32+
Show Similar Problems
33+
34+
* */
35+
36+
public class NonOverlappingIntervals
37+
{
38+
public int eraseOverlapIntervals( Interval[] intervals )
39+
{
40+
if ( intervals.length == 0 )
41+
{
42+
return 0;
43+
}
44+
45+
Arrays.sort( intervals, ( o1, o2 ) -> o1.end - o2.end );
46+
int result = 0;
47+
int end = intervals[0].end;
48+
49+
for ( int i = 1; i < intervals.length; i++ )
50+
{
51+
if ( intervals[i].start >= end )
52+
{
53+
end = intervals[i].end;
54+
result++;
55+
}
56+
}
57+
58+
return intervals.length - result;
59+
}
60+
}

0 commit comments

Comments
 (0)