|
1 | 1 | package easy; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | + |
3 | 5 | /* |
4 | 6 | Given an array of meeting time intervals where intervals[i] = [starti, endi], |
5 | 7 | determine if a person could attend all meetings. |
|
16 | 18 | public class MeetingRooms252 { |
17 | 19 |
|
18 | 20 | // O(N^2) Time | O(1) Space |
19 | | - public static boolean canAttendMeetings(int[][] intervals) { |
| 21 | + public static boolean canAttendMeetings1(int[][] intervals) { |
20 | 22 |
|
21 | 23 | for (int id = 0; id < intervals.length; id++) { |
22 | 24 |
|
@@ -56,14 +58,29 @@ private static boolean isOverlapping(int start1, int end1, int start2, int end2) |
56 | 58 | return false; |
57 | 59 | } |
58 | 60 |
|
| 61 | + // O(NlogN) Time | O(1) Space |
| 62 | + public static boolean canAttendMeetings(int[][] intervals) { |
| 63 | + |
| 64 | + Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); |
| 65 | + |
| 66 | + for (int id = 0; id < intervals.length - 1; id++) { |
| 67 | + if (intervals[id][1] > intervals[id + 1][0]) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
59 | 75 | public static void main(String[] args) { |
60 | 76 |
|
61 | | - // test cases |
| 77 | + /* test cases */ |
62 | 78 | // [[8,11],[17,20],[17,20]] |
63 | 79 | // [[13,15],[1,13]] |
64 | 80 | // { 7, 10 }, { 2, 4 } |
65 | 81 |
|
66 | 82 | int[][] intervals = { { 0, 30 }, { 5, 10 }, { 15, 20 } }; // false |
| 83 | + System.out.println(canAttendMeetings1(intervals)); |
67 | 84 | System.out.println(canAttendMeetings(intervals)); |
68 | 85 | } |
69 | 86 | } |
0 commit comments