Programming & Development / April 10, 2025

LeetCode 252: Meeting Rooms – Check for Conflicts with Python Step-by-Step

LeetCode 252 Meeting Rooms Python interval overlap sort intervals greedy algorithm coding interview schedule conflict meeting scheduler interval problems

Introduction

LeetCode 252: Meeting Rooms is a classic interval problem that tests your ability to detect overlaps between intervals. This is common in scheduling systems and calendar apps.

The goal is to determine if a person can attend all meetings without any overlaps.

Problem Statement

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

Example:

lua

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

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

Step-by-Step Solution in Python

Step 1: Understand the Problem

To avoid overlap:

  • One meeting must end before the next one starts
  • So if we sort intervals by start time, we only need to compare adjacent meetings

Step 2: Sort the Intervals

Sort intervals by their start value.

Step 3: Compare Adjacent Intervals

If start_time[i] < end_time[i-1], there's an overlap → return False.

If all adjacent meetings are safe, return True.

Step 4: Code It

python

def canAttendMeetings(intervals: list[list[int]]) -> bool:
    # Sort intervals by start time
    intervals.sort(key=lambda x: x[0])

    for i in range(1, len(intervals)):
        prev_end = intervals[i - 1][1]
        curr_start = intervals[i][0]
        
        # Check if current meeting starts before the previous ends
        if curr_start < prev_end:
            return False

    return True

Explanation of the Code

  1. Sorting:
  • We sort intervals by their start time to bring overlapping ones next to each other.
  1. Comparison:
  • Loop from the second interval onward.
  • Check if the current meeting starts before the previous meeting ends.
  • If yes, return False.
  1. Return True:
  • If no overlaps are found, all meetings can be attended.

Example Walkthrough

Input: [[0, 30], [5, 10], [15, 20]]

After sorting: [[0, 30], [5, 10], [15, 20]]

  • 5 < 30 → Overlap → Return False

Time and Space Complexity

  • Time Complexity: O(n log n) for sorting + O(n) for comparison = O(n log n)
  • Space Complexity: O(1), in-place sorting (or O(n) depending on the sort implementation)

Conclusion

LeetCode 252 is a simple yet essential problem to understand interval overlap detection. It teaches how sorting can reduce complex comparisons into efficient, linear-time checks.

It’s also a building block for harder problems like:

  • 253. Meeting Rooms II
  • 56. Merge Intervals

Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex