Meeting Room 1 and Meeting Room 2 of Leetcode

                                    Meeting Room 1 and Meeting Room 2 of Leetcode


Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: false

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: true
Solution: Sort according to Ending time. public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (int[] a, int[] b) -> {
return a[1] - b[1];
});
int n = intervals.length;
for(int i=1;i<n;i++){
int st = intervals[i][0];
int en = intervals[i][1];
if(st < intervals[i-1][1]){
return false;
}
}
return true;
}



Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

 

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: 1

class Solution {
public int minMeetingRooms(int[][] intervals) {
List<int[]> events = new ArrayList<>();
for(int i=0;i<intervals.length;i++) {
events.add(new int[]{intervals[i][0], i, 0});
events.add(new int[]{intervals[i][1], i, 1});
}

Collections.sort(events, (a, b) -> {
if(a[0]==b[0]) {
return b[2]-a[2];
}
return a[0] - b[0];
});

int rooms = 0;
int maxRooms = 0;

// Iterate through the events
for (int[] event : events) {
if (event[2] == 0) { // Start of a meeting
rooms++;
} else { // End of a meeting
rooms--;
}
maxRooms = Math.max(maxRooms, rooms);
}

return maxRooms;
}
}
Extended version of this question is https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/description/

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.