Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit08dfc88

Browse files
non-overlapping intervals
1 parenta8656bc commit08dfc88

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagemedium;
2+
3+
importjava.util.Arrays;
4+
importjava.util.Collections;
5+
importjava.util.Comparator;
6+
importjava.util.List;
7+
8+
importclasses.Interval;
9+
10+
publicclassNonOverlappingIntervals {
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+
publicstaticinteraseOverlapIntervals(Interval[]intervals) {
15+
Collections.sort(Arrays.asList(intervals),newComparator<Interval>(){
16+
@Override
17+
publicintcompare(Intervalo1,Intervalo2) {
18+
if(o1.end !=o2.end)returno1.end -o2.end;
19+
elsereturno2.start -o1.start;
20+
}
21+
});
22+
intend =Integer.MIN_VALUE;
23+
intcount =0;
24+
for(Intervalinterval :intervals){
25+
if(interval.start >=end)end =interval.end;
26+
elsecount++;
27+
}
28+
returncount;
29+
}
30+
31+
publicstaticvoidmain(String...args){
32+
//[[1,100],[11,22],[1,11],[2,12]]
33+
Intervalinterval1 =newInterval(1,100);
34+
Intervalinterval2 =newInterval(11,22);
35+
Intervalinterval3 =newInterval(1,11);
36+
Intervalinterval4 =newInterval(2,12);
37+
Interval[]intervals =newInterval[]{interval1,interval2,interval3,interval4};
38+
39+
40+
System.out.println(eraseOverlapIntervals(intervals));
41+
}
42+
43+
}

‎README.md

Lines changed: 2 additions & 1 deletion
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 ArrayElementss](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 ArrayElements](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)

[8]ページ先頭

©2009-2025 Movatter.jp