Skip to content

Commit 08dfc88

Browse files
non-overlapping intervals
1 parent a8656bc commit 08dfc88

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package medium;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
import classes.Interval;
9+
10+
public class NonOverlappingIntervals {
11+
/**Looked at these two posts: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain
12+
* and https://discuss.leetcode.com/topic/65594/java-least-is-most
13+
* Sort the intervals by their end time, if equal, then sort by their start time.*/
14+
public static int eraseOverlapIntervals(Interval[] intervals) {
15+
Collections.sort(Arrays.asList(intervals), new Comparator<Interval>(){
16+
@Override
17+
public int compare(Interval o1, Interval o2) {
18+
if(o1.end != o2.end) return o1.end - o2.end;
19+
else return o2.start - o1.start;
20+
}
21+
});
22+
int end = Integer.MIN_VALUE;
23+
int count = 0;
24+
for(Interval interval : intervals){
25+
if(interval.start >= end) end = interval.end;
26+
else count++;
27+
}
28+
return count;
29+
}
30+
31+
public static void main(String...args){
32+
//[[1,100],[11,22],[1,11],[2,12]]
33+
Interval interval1 = new Interval(1,100);
34+
Interval interval2 = new Interval(11,22);
35+
Interval interval3 = new Interval(1,11);
36+
Interval interval4 = new Interval(2,12);
37+
Interval[] intervals = new Interval[]{interval1, interval2, interval3, interval4};
38+
39+
40+
System.out.println(eraseOverlapIntervals(intervals));
41+
}
42+
43+
}

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# fishercoderLeetcode
22
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
33
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
4-
|453|[Minimum Moves to Equal Array Elementss](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../blob/master/EASY/src/easy/MinimumMovestoEqualArrayElements.java)| O(n)|O(1) | Easy|
4+
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../blob/master/EASY/src/easy/MinimumMovestoEqualArrayElements.java)| O(n)|O(1) | Easy|
55
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../../blob/master/EASY/src/easy/NumberofBoomerangs.java)| O(n^2)|O(n) | Easy| HashMap
66
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../blob/master/EASY/src/easy/ArrangingCoins.java)| O(n)|O(1) | Easy|
7+
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../../blob/master/MEDIUM/src/medium/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
78
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
89
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS
910
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|[Solution](../../blob/master/EASY/src/easy/AddStrings.java)| O(n)|O(1) | Easy|

0 commit comments

Comments
 (0)