-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterview9.java
More file actions
38 lines (34 loc) · 961 Bytes
/
Interview9.java
File metadata and controls
38 lines (34 loc) · 961 Bytes
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
package pramp;
/**
* Given time availabilities for two people and a duration for meeting between
* them, find the earliest available time slot where they can conduct schedule
* the meeting. If there is no such time slot then return an empty array.
*
* @author: shivam.maharshi
*/
public class Interview9 {
public static int[] get(int[][] ta, int[][] tb, int dur) {
int[] r = new int[2];
if (ta == null || ta.length == 0 || tb == null || tb.length == 0)
return r;
int i = 0, j = 0;
while (i < ta.length && j < tb.length) {
int start = Math.max(ta[i][0], tb[j][0]), end = Math.min(ta[i][1], tb[j][1]);
if (end - start >= dur) {
r[0] = start;
r[1] = end;
return r;
} else {
if (i == ta.length - 1)
j++;
else if (j == tb.length - 1)
i++;
else if (ta[i][1] < tb[j][1])
i++;
else
j++;
}
}
return r;
}
}