-
Notifications
You must be signed in to change notification settings - Fork 5
/
Day-27 Find Right Interval
56 lines (51 loc) · 1.81 KB
/
Day-27 Find Right Interval
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public int[] findRightInterval(int[][] intervals) {
int[] res = new int[intervals.length];
for (int i = 0; i < intervals.length; i++) {
int min = Integer.MAX_VALUE;
int minindex = -1;
for (int j = 0; j < intervals.length; j++) {
if (intervals[j][0] >= intervals[i][1] && intervals[j][0] < min) {
min = intervals[j][0];
minindex = j;
}
}
res[i] = minindex;
}
return res;
}
}
class Solution {
public int[] findRightInterval(int[][] intervals) {
int[] result = new int[intervals.length];
TreeMap<Integer, Integer> intervalMap = new TreeMap<>();
for (int i = 0; i < intervals.length; ++i) {
intervalMap.put(intervals[i][0], i);
}
for (int i = 0; i < intervals.length; ++i) {
Map.Entry<Integer, Integer> entry = intervalMap.ceilingEntry(intervals[i][1]);
result[i] = (entry != null) ? entry.getValue() : -1;
}
return result;
}
}
public class Solution {
public int[] findRightInterval(int[][] intervals) {
int[][] endIntervals = Arrays.copyOf(intervals, intervals.length);
HashMap<int[], Integer> hash = new HashMap<>();
for (int i = 0; i < intervals.length; i++) {
hash.put(intervals[i], i);
}
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
Arrays.sort(endIntervals, (a, b) -> a[1] - b[1]);
int j = 0;
int[] res = new int[intervals.length];
for (int i = 0; i < endIntervals.length; i++) {
while (j < intervals.length && intervals[j][0] < endIntervals[i][1]) {
j++;
}
res[hash.get(endIntervals[i])] = j == intervals.length ? -1 : hash.get(intervals[j]);
}
return res;
}
}