Skip to content

Commit 34764c5

Browse files
committed
Update meeting rooms logic
1 parent 5ba4795 commit 34764c5

File tree

7 files changed

+144
-60
lines changed

7 files changed

+144
-60
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ List of Programs related to data structures and algorithms
234234

235235
3. Non-overlapping intervals: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/nonOverlappingIntervals/nonOverlappingIntervals.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/nonOverlappingIntervals/nonOverlappingIntervals.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/nonOverlappingIntervals/nonOverlappingIntervals.md)
236236

237-
4. Meeting rooms: [JavaScript](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/meetingRooms.js)
237+
4. Meeting rooms: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/meetingRooms/meetingRooms.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/meetingRooms/meetingRooms.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/meetingRooms/meetingRooms.md)
238238

239239
5. Meeting rooms 2: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/meetingRooms2.js)
240240

src/java1/algorithms/interval/MeetingRooms.java

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//TC: O(n logn)+ O(n) SC:O(1)
2+
3+
package java1.algorithms.interval.meetingRooms;
4+
import java.util.*;
5+
6+
class Interval {
7+
int start, end;
8+
Interval(int start, int end) {
9+
this.start = start;
10+
this.end = end;
11+
}
12+
}
13+
14+
public class MeetingRooms {
15+
16+
private static boolean canAttendMeetings(List<Interval> intervals) {
17+
Collections.sort(intervals, (a, b) -> a.start - b.start);
18+
19+
for(int i=1; i< intervals.size(); i++) {
20+
int startTime = intervals.get(i).start;
21+
int prevEndTime = intervals.get(i-1).end;
22+
23+
if(prevEndTime > startTime) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
30+
public static void main(String[] args) {
31+
List<Interval> intervals1 = new ArrayList<Interval>();
32+
intervals1.add(new Interval(1, 3));
33+
intervals1.add(new Interval(3, 7));
34+
intervals1.add(new Interval(6, 8));
35+
intervals1.add(new Interval(8, 10));
36+
intervals1.add(new Interval(10, 12));
37+
38+
List<Interval> intervals2 = new ArrayList<Interval>();
39+
intervals2.add(new Interval(1, 5));
40+
intervals2.add(new Interval(5, 7));
41+
intervals2.add(new Interval(8, 10));
42+
43+
System.out.println(canAttendMeetings(intervals1));
44+
System.out.println(canAttendMeetings(intervals2));
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem:**
2+
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
3+
4+
Remember that [[1, 4],[4, 7]] will not conflict at 4.
5+
6+
**Examples:**
7+
Input: intervals = [[1,3],[3,7],[6,8],[8, 10], [10, 12]]
8+
Output: false
9+
Explanation:
10+
The meeting intervals [3,7], [6,8] will conflict
11+
12+
Input: intervals = [[1,5], [5,7], [8, 10]]
13+
Output: true
14+
Explanation:
15+
The three meeting intervals will not conflict
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of greedy approach. The algorithmic approach can be summarized as follows:
19+
20+
1. First, sort the list of intervals based on their start times. This step helps to keep the intervals arranged in a sequential order and makes it easy to check for any overlaps in the list.
21+
22+
2. Iterate through the list of intervals. For each iteration, keep the current start time(i.e, `startTime`) and previous end time(i.e, `prevEndTime`) values to compare start time with previous meeting's end time.
23+
24+
**Note:** Remember that the main idea behind this solution is to verify that each meeting ends before the next meeting starts.
25+
26+
3. If the start time is less than the end time of the previous interval, then there is an overlap between the intervals. In this case, a person cannot attend all meetings. So you need to return `false` immeidately.
27+
28+
4. If the start time is greater than the previous interval's end time, then there is no overlap between the intervals. In this case, you need to proceed with next set of intervals to find any overlap between the meetings.
29+
30+
5. Repeat steps 2-4 until there is no overlap exists in the list or all intervals have been traversed.
31+
32+
6. Return `true` indicating that a person can attend all meetings.
33+
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of O(n logn) + O(n) ~= O(n), where n is the number of intervals. This is because sort operation typically takes a time complexity of O(n log n) for sorting the list. Thereafter, iteration over the intervals take a time complexity of O(n). So the total time complexity simplifies to O(n log n).
37+
38+
The space complexity of this algorithm is O(1) due to no additional data structure used.

src/javascript/algorithms/interval/meetingRooms.js

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//TC: O(n logn)+ O(n) SC:O(1)
2+
3+
function canAttendMeetings(intervals) {
4+
intervals.sort((a, b) => a[0] - b[0]);
5+
6+
for(let i=1; i< intervals.length; i++) {
7+
let startTime = intervals[i][0];
8+
let prevEndTime = intervals[i-1][1];
9+
10+
if(prevEndTime > startTime) {
11+
return false;
12+
}
13+
}
14+
15+
return true;
16+
}
17+
18+
let intervals1 = [[1,3], [3,7], [6, 8], [8, 10], [10, 12]];
19+
let intervals2 = [[1,5], [5,7], [8, 10]];
20+
console.log(canAttendMeetings(intervals1));
21+
console.log(canAttendMeetings(intervals2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem:**
2+
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
3+
4+
Remember that [[1, 4],[4, 7]] will not conflict at 4.
5+
6+
**Examples:**
7+
Input: intervals = [[1,3],[3,7],[6,8],[8, 10], [10, 12]]
8+
Output: false
9+
Explanation:
10+
The meeting intervals [3,7], [6,8] will conflict
11+
12+
Input: intervals = [[1,5], [5,7], [8, 10]]
13+
Output: true
14+
Explanation:
15+
The three meeting intervals will not conflict
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of greedy approach. The algorithmic approach can be summarized as follows:
19+
20+
1. First, sort the list of intervals based on their start times. This step helps to keep the intervals arranged in a sequential order and makes it easy to check for any overlaps in the list.
21+
22+
2. Iterate through the list of intervals. For each iteration, keep the current start time(i.e, `startTime`) and previous end time(i.e, `prevEndTime`) values to compare start time with previous meeting's end time.
23+
24+
**Note:** Remember that the main idea behind this solution is to verify that each meeting ends before the next meeting starts.
25+
26+
3. If the start time is less than the end time of the previous interval, then there is an overlap between the intervals. In this case, a person cannot attend all meetings. So you need to return `false` immeidately.
27+
28+
4. If the start time is greater than the previous interval's end time, then there is no overlap between the intervals. In this case, you need to proceed with next set of intervals to find any overlap between the meetings.
29+
30+
5. Repeat steps 2-4 until there is no overlap exists in the list or all intervals have been traversed.
31+
32+
6. Return `true` indicating that a person can attend all meetings.
33+
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of O(n logn) + O(n) ~= O(n), where n is the number of intervals. This is because sort operation typically takes a time complexity of O(n log n) for sorting the list. Thereafter, iteration over the intervals take a time complexity of O(n). So the total time complexity simplifies to O(n log n).
37+
38+
The space complexity of this algorithm is O(1) due to no additional data structure used.

0 commit comments

Comments
 (0)