|
| 1 | +package medium300; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +public class LectureSchedule { |
| 6 | + |
| 7 | + public int maxLectures(int[] start, int[] end) { |
| 8 | + |
| 9 | + Arrays.sort(start); |
| 10 | + Arrays.sort(end); |
| 11 | + |
| 12 | + int last = 0; |
| 13 | + int st = 0, en = 0; |
| 14 | + int res = 0; |
| 15 | + |
| 16 | + while (st < start.length && en < start.length) { |
| 17 | + |
| 18 | + if (end[en] <= start[st]) { |
| 19 | + en++; |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + if (start[st] < last) { |
| 24 | + st++; |
| 25 | + continue; |
| 26 | + } |
| 27 | + last = end[en]; |
| 28 | + res++; |
| 29 | + st++; |
| 30 | + en++; |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + return res; |
| 35 | + } |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + LectureSchedule l = new LectureSchedule(); |
| 39 | + System.out.println(l.maxLectures(new int[] { 2, 20, 35, 44 }, new int[] { 39, 41, 46, 49 })); |
| 40 | + System.out.println( |
| 41 | + l.maxLectures(new int[] { 2, 5, 16, 20, 46, 48, 50 }, new int[] { 21, 33, 39, 40, 48, 49, 50 })); |
| 42 | + System.out.println( |
| 43 | + l.maxLectures(new int[] { 21, 23, 28, 34, 40, 41, 49 }, new int[] { 32, 33, 41, 47, 48, 49, 50 })); |
| 44 | + System.out.println( |
| 45 | + l.maxLectures(new int[] { 53, 58, 84, 163, 329, 335, 475, 486, 595, 727, 938, 962, 968, 981, 998 }, |
| 46 | + new int[] { 144, 456, 513, 520, 592, 715, 781, 821, 844, 963, 970, 976, 983, 997, 999 })); |
| 47 | + } |
| 48 | +} |
0 commit comments