|
| 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 | +*/ |
1 | 8 | class Pramp { |
2 | 9 | public static void main(String[] args) { |
3 | 10 | String pramp = "Practice Makes Perfect"; |
4 | 11 | System.out.println(pramp); |
5 | 12 | } |
6 | 13 |
|
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 |
16 | 14 | public static int[] get (int[][] ta, [][] tb, int dur) { |
17 | 15 | int[] r = new int[2]; |
18 | 16 | if (ta == null || ta.length() == 0 || tb == null || tb.length() ==0) |
19 | 17 | return r; |
20 | 18 | int i=0, j=0; |
21 | 19 |
|
22 | 20 | 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; |
26 | 25 | return r; |
27 | 26 | } else { |
28 | 27 | if (i == ta.length() -1) |
29 | 28 | j++; |
30 | 29 | else if (j == tb.length() - 1) |
31 | 30 | i++; |
32 | | - else if (incrA(ta, tb, i, j)) |
| 31 | + else if (ta[i][1] < tb[j][1]) |
33 | 32 | i++; |
34 | 33 | else |
35 | 34 | j++; |
36 | 35 | } |
37 | | - } |
38 | | - |
| 36 | + } |
39 | 37 | return r; |
40 | 38 | } |
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 | | - |
50 | 39 | } |
0 commit comments