LeetCode 0252. Meeting Rooms Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0252. Meeting Rooms

Description

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

 

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti < endi <= 106

Solutions

Solution 1: Sorting

We sort the meetings based on their start times, and then iterate through the sorted meetings. If the start time of the current meeting is less than the end time of the previous meeting, it indicates that there is an overlap between the two meetings, and we return false. Otherwise, we continue iterating.

If no overlap is found by the end of the iteration, we return true.

The time complexity is O(n × log n), and the space complexity is O(log n), where n is the number of meetings.

PythonJavaC++GoTypeScriptRust
class Solution: def canAttendMeetings(self, intervals: List[List[int]]) -> bool: intervals.sort() return all(a[1] <= b[0] for a, b in pairwise(intervals))(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !