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

Commitf44ceca

Browse files
find right interval
1 parent08dfc88 commitf44ceca

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
packagemedium;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.Collections;
6+
importjava.util.Comparator;
7+
importjava.util.HashMap;
8+
importjava.util.List;
9+
importjava.util.Map;
10+
11+
importutils.CommonUtils;
12+
importclasses.Interval;
13+
14+
publicclassFindRightInterval {
15+
16+
publicstaticint[]findRightInterval(Interval[]intervals) {
17+
if(intervals ==null ||intervals.length ==0)returnnewint[0];
18+
int[]result =newint[intervals.length];
19+
result[0] = -1;
20+
Intervallast =intervals[intervals.length-1];
21+
Intervalfirst =intervals[0];
22+
Map<Interval,Integer>map =newHashMap();
23+
for(inti =0;i <intervals.length;i++){
24+
map.put(intervals[i],i);
25+
}
26+
27+
Collections.sort(Arrays.asList(intervals),newComparator<Interval>(){
28+
@Override
29+
publicintcompare(Intervalo1,Intervalo2) {
30+
returno1.start -o2.start;
31+
}
32+
});
33+
34+
for(inti =1;i <intervals.length;i++){
35+
//TODO: use binary search for the minimum start interval for interval[i-1] instead of a while loop
36+
inttmp =i-1,tmpI =i;
37+
while(tmpI <intervals.length &&intervals[tmpI].start <intervals[tmp].end)tmpI++;
38+
if(tmpI <intervals.length)result[map.get(intervals[tmp])] =map.get(intervals[tmpI]);
39+
elseresult[map.get(intervals[tmp])] = -1;
40+
}
41+
if(result[intervals.length-1] ==0 &&last.end >first.start)result[intervals.length-1] = -1;
42+
returnresult;
43+
}
44+
45+
46+
publicstaticvoidmain(String...args){
47+
Interval[]intervals =newInterval[3];
48+
// intervals[0] = new Interval(1,4);
49+
// intervals[1] = new Interval(2,3);
50+
// intervals[2] = new Interval(3,4);
51+
52+
intervals[0] =newInterval(3,4);
53+
intervals[1] =newInterval(2,3);
54+
intervals[2] =newInterval(1,2);
55+
findRightInterval(intervals);
56+
}
57+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
|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+
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../../blob/master/MEDIUM/src/medium/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
78
|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
89
|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
910
|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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp