-
Notifications
You must be signed in to change notification settings - Fork 0
/
436.寻找右区间.java
54 lines (49 loc) · 1.39 KB
/
436.寻找右区间.java
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
import java.util.Arrays;
import java.util.Comparator;
/*
* @lc app=leetcode.cn id=436 lang=java
*
* [436] 寻找右区间
*/
// @lc code=start
class Solution {
public int[] findRightInterval(int[][] intervals) {
int n = intervals.length;
int[][] clone = new int[n][3];
for(int i=0;i<n;i++){
clone[i] = new int[3];
clone[i][0] = i;
clone[i][1] = intervals[i][0];
clone[i][2] = intervals[i][1];
}
Arrays.sort(clone, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1]-o2[1];
}
});
int[] res = new int[n];
Arrays.fill(res, -1);
for(int i=0;i<n;i++){
int j = i,k = n-1;
// while(j<n&&clone[j][1]<clone[i][2])j++;
//二分法找到第一个clone[j][1]>=clone[i][2]的j
while(j<k){
int mid = (j+k)/2;
if(clone[mid][1]==clone[i][2]){
j=mid;
break;
}else if(clone[mid][1]>clone[i][2]){
k = mid;
}else{
j = mid+1;
}
}
if(j<n&&clone[j][1]>=clone[i][2]){
res[clone[i][0]] = clone[j][0];
}
}
return res;
}
}
// @lc code=end