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
whereintervals[i] = [starti, endi]
, return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]] Output: 2Example 2:
Input: intervals = [[7,10],[2,4]] Output: 1class 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 eventsfor (int[] event : events) {if (event[2] == 0) { // Start of a meetingrooms++;} else { // End of a meetingrooms--;}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
Post a Comment