Skip to content

Commit cea130a

Browse files
committed
LC#252, optimal-find if a person could attend all meetings, O(NlogN) Time, O(1) Space
1 parent 9baac4c commit cea130a

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Leetcode/easy/MeetingRooms252.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package easy;
22

3+
import java.util.Arrays;
4+
35
/*
46
Given an array of meeting time intervals where intervals[i] = [starti, endi],
57
determine if a person could attend all meetings.
@@ -16,7 +18,7 @@
1618
public class MeetingRooms252 {
1719

1820
// O(N^2) Time | O(1) Space
19-
public static boolean canAttendMeetings(int[][] intervals) {
21+
public static boolean canAttendMeetings1(int[][] intervals) {
2022

2123
for (int id = 0; id < intervals.length; id++) {
2224

@@ -56,14 +58,29 @@ private static boolean isOverlapping(int start1, int end1, int start2, int end2)
5658
return false;
5759
}
5860

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+
5975
public static void main(String[] args) {
6076

61-
// test cases
77+
/* test cases */
6278
// [[8,11],[17,20],[17,20]]
6379
// [[13,15],[1,13]]
6480
// { 7, 10 }, { 2, 4 }
6581

6682
int[][] intervals = { { 0, 30 }, { 5, 10 }, { 15, 20 } }; // false
83+
System.out.println(canAttendMeetings1(intervals));
6784
System.out.println(canAttendMeetings(intervals));
6885
}
6986
}

0 commit comments

Comments
 (0)