Skip to content

Commit 91bb8ce

Browse files
refactor 436
1 parent d78784b commit 91bb8ce

File tree

2 files changed

+94
-39
lines changed

2 files changed

+94
-39
lines changed

src/main/java/com/fishercoder/solutions/_436.java

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,75 @@
77
import java.util.HashMap;
88
import java.util.Map;
99

10+
/**436. Find Right Interval
11+
*
12+
* Given a set of intervals, for each of the interval i, check if there exists an interval j whose start
13+
* point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
14+
*
15+
* For any interval i, you need to store the minimum interval j's index,
16+
* which means that the interval j has the minimum start point to build the "right" relationship for interval i.
17+
* If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
18+
*
19+
* Note:
20+
*
21+
* You may assume the interval's end point is always bigger than its start point.
22+
* You may assume none of these intervals have the same start point.
23+
*
24+
* Example 1:
25+
* Input: [ [1,2] ]
26+
* Output: [-1]
27+
* Explanation: There is only one interval in the collection, so it outputs -1.
28+
*
29+
* Example 2:
30+
* Input: [ [3,4], [2,3], [1,2] ]
31+
* Output: [-1, 0, 1]
32+
* Explanation: There is no satisfied "right" interval for [3,4].
33+
* For [2,3], the interval [3,4] has minimum-"right" start point;
34+
* For [1,2], the interval [2,3] has minimum-"right" start point.
35+
*
36+
* Example 3:
37+
* Input: [ [1,4], [2,3], [3,4] ]
38+
* Output: [-1, 2, -1]
39+
* Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
40+
* For [2,3], the interval [3,4] has minimum-"right" start point.
41+
* */
1042

1143
public class _436 {
1244

13-
public static int[] findRightInterval(Interval[] intervals) {
14-
if (intervals == null || intervals.length == 0) {
15-
return new int[0];
16-
}
17-
int[] result = new int[intervals.length];
18-
result[0] = -1;
19-
Interval last = intervals[intervals.length - 1];
20-
Interval first = intervals[0];
21-
Map<Interval, Integer> map = new HashMap();
22-
for (int i = 0; i < intervals.length; i++) {
23-
map.put(intervals[i], i);
24-
}
45+
public static class Solution1 {
46+
public int[] findRightInterval(Interval[] intervals) {
47+
if (intervals == null || intervals.length == 0) {
48+
return new int[0];
49+
}
50+
int[] result = new int[intervals.length];
51+
result[0] = -1;
52+
Interval last = intervals[intervals.length - 1];
53+
Interval first = intervals[0];
54+
Map<Interval, Integer> map = new HashMap();
55+
for (int i = 0; i < intervals.length; i++) {
56+
map.put(intervals[i], i);
57+
}
2558

26-
Collections.sort(Arrays.asList(intervals), (o1, o2) -> o1.start - o2.start);
59+
Collections.sort(Arrays.asList(intervals), (o1, o2) -> o1.start - o2.start);
2760

28-
for (int i = 1; i < intervals.length; i++) {
29-
//TODO: use binary search for the minimum start interval for interval[i-1] instead of a while loop
30-
int tmp = i - 1;
31-
int tmpI = i;
32-
while (tmpI < intervals.length && intervals[tmpI].start < intervals[tmp].end) {
33-
tmpI++;
61+
for (int i = 1; i < intervals.length; i++) {
62+
//TODO: use binary search for the minimum start interval for interval[i-1] instead of a while loop
63+
int tmp = i - 1;
64+
int tmpI = i;
65+
while (tmpI < intervals.length && intervals[tmpI].start < intervals[tmp].end) {
66+
tmpI++;
67+
}
68+
if (tmpI < intervals.length) {
69+
result[map.get(intervals[tmp])] = map.get(intervals[tmpI]);
70+
} else {
71+
result[map.get(intervals[tmp])] = -1;
72+
}
3473
}
35-
if (tmpI < intervals.length) {
36-
result[map.get(intervals[tmp])] = map.get(intervals[tmpI]);
37-
} else {
38-
result[map.get(intervals[tmp])] = -1;
74+
if (result[intervals.length - 1] == 0 && last.end > first.start) {
75+
result[intervals.length - 1] = -1;
3976
}
77+
return result;
4078
}
41-
if (result[intervals.length - 1] == 0 && last.end > first.start) {
42-
result[intervals.length - 1] = -1;
43-
}
44-
return result;
4579
}
4680

47-
48-
public static void main(String... args) {
49-
Interval[] intervals = new Interval[3];
50-
// intervals[0] = new Interval(1,4);
51-
// intervals[1] = new Interval(2,3);
52-
// intervals[2] = new Interval(3,4);
53-
54-
intervals[0] = new Interval(3, 4);
55-
intervals[1] = new Interval(2, 3);
56-
intervals[2] = new Interval(1, 2);
57-
findRightInterval(intervals);
58-
}
5981
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.Interval;
4+
import com.fishercoder.solutions._436;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _436Test {
9+
private static _436.Solution1 solution1;
10+
11+
@BeforeClass
12+
public static void setup() {
13+
solution1 = new _436.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
Interval[] intervals = new Interval[3];
19+
intervals[0] = new Interval(3, 4);
20+
intervals[1] = new Interval(2, 3);
21+
intervals[2] = new Interval(1, 2);
22+
solution1.findRightInterval(intervals);
23+
}
24+
25+
@Test
26+
public void test2() {
27+
Interval[] intervals = new Interval[3];
28+
intervals[0] = new Interval(1, 4);
29+
intervals[1] = new Interval(2, 3);
30+
intervals[2] = new Interval(3, 4);
31+
solution1.findRightInterval(intervals);
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy