Skip to content

Commit df8a6ea

Browse files
Update Interview9
1 parent efabd17 commit df8a6ea

1 file changed

Lines changed: 13 additions & 24 deletions

File tree

src/main/java/pramp/Interview9

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
1+
/*
2+
* Given time availabilities for two people and a duration for meeting between them, find the
3+
* earliest available time slot where they can conduct schedule the meeting. If there is no
4+
* such time slot then return an empty array.
5+
*
6+
* @author: shivam.maharshi
7+
*/
18
class Pramp {
29
public static void main(String[] args) {
310
String pramp = "Practice Makes Perfect";
411
System.out.println(pramp);
512
}
613

7-
timesA = {[1, 3] , [6, 7], [10, 15]}
8-
timesB = {[4, 5] , [6, 11], [12, 20]}
9-
dur = 3;
10-
11-
// 1, 5
12-
// 2, 5
13-
// 3
14-
15-
// n * 2 // m * 2
1614
public static int[] get (int[][] ta, [][] tb, int dur) {
1715
int[] r = new int[2];
1816
if (ta == null || ta.length() == 0 || tb == null || tb.length() ==0)
1917
return r;
2018
int i=0, j=0;
2119

2220
while (i < ta.legnth() && j < tb.length()) {
23-
if (hasSlot(ta[i], tb[j], i, j, dur)) {
24-
r[0] = Math.max(ta[i][0], tb[j][0]); // Start
25-
r[1] = Math.min(ta[i][1], tb[j][1]); // End
21+
int start = Math.max(ta[i][0], tb[j][0]), end = Math.min(ta[i][1], tb[j][1]);
22+
if (end - start >= dur) {
23+
r[0] = start;
24+
r[1] = end;
2625
return r;
2726
} else {
2827
if (i == ta.length() -1)
2928
j++;
3029
else if (j == tb.length() - 1)
3130
i++;
32-
else if (incrA(ta, tb, i, j))
31+
else if (ta[i][1] < tb[j][1])
3332
i++;
3433
else
3534
j++;
3635
}
37-
}
38-
36+
}
3937
return r;
4038
}
41-
42-
public static boolean hasSlot(int[][] ta, int[][] tb, int i, int j, int dur) {
43-
return Math.min(ta[i][1], tb[j][1]) - Math.max(ta[i][0], tb[j][0]) >= dur;
44-
}
45-
46-
public static boolean incrA(int[][] ta, int[][] tb, int i, int j) {
47-
return ta[i][1] < tb[j][1];
48-
}
49-
5039
}

0 commit comments

Comments
 (0)