|
| 1 | +''' |
| 2 | +시간 복잡도: O(n log n) |
| 3 | +공간 복잡도: O(n) |
| 4 | +''' |
| 5 | +from typing import List |
| 6 | +from heapq import heappush, heappop |
| 7 | + |
| 8 | +class Interval(object): |
| 9 | + def __init__(self, start, end): |
| 10 | + self.start = start |
| 11 | + self.end = end |
| 12 | + |
| 13 | +class Solution: |
| 14 | + def min_meeting_rooms(self, intervals: List[Interval]) -> int: |
| 15 | + intervals.sort(key=lambda x:x.start) |
| 16 | + ends = [] |
| 17 | + |
| 18 | + for interval in intervals: |
| 19 | + if ends and ends[0] <= interval.start: |
| 20 | + heappop(ends) |
| 21 | + |
| 22 | + heappush(ends, interval.end) |
| 23 | + |
| 24 | + return len(ends) |
| 25 | + |
| 26 | + |
| 27 | +def run_tests(): |
| 28 | + solution = Solution() |
| 29 | + |
| 30 | + test_cases = [ |
| 31 | + # Test Case 1: 문제의 Example 1 |
| 32 | + { |
| 33 | + "input": [Interval(0, 30), Interval(5, 10), Interval(15, 20)], |
| 34 | + "expected": 2, |
| 35 | + "description": "Example 1: [(0,30), (5,10), (15,20)] - 2 rooms needed" |
| 36 | + }, |
| 37 | + # Test Case 2: 문제의 Example 2 |
| 38 | + { |
| 39 | + "input": [Interval(2, 7)], |
| 40 | + "expected": 1, |
| 41 | + "description": "Example 2: [(2,7)] - 1 room needed" |
| 42 | + }, |
| 43 | + # Test Case 3: 겹치지 않는 회의들 |
| 44 | + { |
| 45 | + "input": [Interval(0, 8), Interval(8, 10), Interval(10, 12)], |
| 46 | + "expected": 1, |
| 47 | + "description": "Non-overlapping meetings: [(0,8), (8,10), (10,12)]" |
| 48 | + }, |
| 49 | + # Test Case 4: 모든 회의가 겹치는 경우 |
| 50 | + { |
| 51 | + "input": [Interval(1, 5), Interval(2, 6), Interval(3, 7)], |
| 52 | + "expected": 3, |
| 53 | + "description": "All overlapping: [(1,5), (2,6), (3,7)]" |
| 54 | + }, |
| 55 | + # Test Case 5: 빈 입력 |
| 56 | + { |
| 57 | + "input": [], |
| 58 | + "expected": 0, |
| 59 | + "description": "Empty input: []" |
| 60 | + }, |
| 61 | + # Test Case 6: 복잡한 경우 |
| 62 | + { |
| 63 | + "input": [Interval(1, 10), Interval(2, 7), Interval(3, 19), Interval(8, 12)], |
| 64 | + "expected": 3, |
| 65 | + "description": "Complex case: [(1,10), (2,7), (3,19), (8,12)]" |
| 66 | + }, |
| 67 | + { |
| 68 | + "input": [Interval(1, 4), Interval(2, 5), Interval(3, 6)], |
| 69 | + "expected": 3, |
| 70 | + "description": "Multiple overlaps: [(1,4), (2,5), (3,6)]" |
| 71 | + }, |
| 72 | + { |
| 73 | + "input": [Interval(1, 10), Interval(2, 3), Interval(4, 5), Interval(6, 7)], |
| 74 | + "expected": 2, |
| 75 | + "description": "Short and long meetings: [(1,10), (2,3), (4,5), (6,7)]" |
| 76 | + }, |
| 77 | + { |
| 78 | + "input": [Interval(1, 5), Interval(5, 10), Interval(10, 15), Interval(2, 7)], |
| 79 | + "expected": 2, |
| 80 | + "description": "Mixed overlaps: [(1,5), (5,10), (10,15), (2,7)]" |
| 81 | + } |
| 82 | + ] |
| 83 | + |
| 84 | + # 테스트 실행 |
| 85 | + for i, test in enumerate(test_cases, 1): |
| 86 | + intervals = test["input"] |
| 87 | + expected = test["expected"] |
| 88 | + result = solution.min_meeting_rooms(intervals) |
| 89 | + |
| 90 | + print(f"Test Case {i}: {test['description']}") |
| 91 | + print(f"Input: {[(interval.start, interval.end) for interval in intervals]}") |
| 92 | + print(f"Expected Output: {expected}") |
| 93 | + print(f"Your Output: {result}") |
| 94 | + print(f"Result: {'✅ PASS' if result == expected else '❌ FAIL'}") |
| 95 | + print("-" * 50) |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + run_tests() |
0 commit comments