|
1 | 1 | """
|
2 | 2 | Constraints:
|
3 |
| -- |
| 3 | +- 0 โค intervals.length โค 10^4 |
| 4 | +- intervals[i].length == 2 |
| 5 | +- 0 โค start_i < end_i โค 10^6 |
| 6 | +- [(0,8), (8,10)] is not conflict at 8 |
4 | 7 |
|
5 |
| -Time Complexity: |
6 |
| -- |
| 8 | +Time Complexity: O(nlogn) |
| 9 | +- ์ ๋ ฌ์ nlogn, ์ํ์ n์ด ํ์ํ๋ฏ๋ก ์ ์ฒด๋ O(nlogn) |
7 | 10 |
|
8 |
| -Space Complexity: |
9 |
| -- |
| 11 | +Space Complexity: O(1) |
| 12 | +- ์ ํด์ง ๊ฐ์์ ๋ณ์ ์ธ์๋ ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์์ |
10 | 13 |
|
11 | 14 | ํ์ด๋ฐฉ๋ฒ:
|
12 |
| -1. |
| 15 | +1. Base case: ๋น ๋ฐฐ์ด/none์ผ ๋ True ๋ฐํ |
| 16 | +2. intervals๋ฅผ ์์์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ |
| 17 | +3. prev_end ์ด๊ธฐํ (์ฒซ ๋ฒ์งธ ๋ฏธํ
์ ์ข
๋ฃ ์๊ฐ) |
| 18 | +4. ๋ ๋ฒ์งธ ๋ฏธํ
๋ถํฐ ์ํํ๋ฉด์: |
| 19 | + - ๋ง์ฝ ํ์ฌ ์์์ (๋ฏธํ
์์ ์๊ฐ)์ด ์ด์ ๋ฏธํ
์ ์ข
๋ฃ ์๊ฐ๋ณด๋ค ์์ผ๋ฉด false ๋ฐํ |
| 20 | + - ๊ทธ๋ ์ง ์์ผ๋ฉด prev_end๋ฅผ ํ์ฌ ๋ฏธํ
์ ์ข
๋ฃ ์๊ฐ์ผ๋ก ์
๋ฐ์ดํธ |
| 21 | +5. ๋ชจ๋ ๋ฏธํ
์ ๊ฒ์ฌํ ํ์๋ ์ถฉ๋์ด ์์ผ๋ฉด true ๋ฐํ |
13 | 22 | """
|
| 23 | +from typing import List |
| 24 | + |
| 25 | +class Interval: |
| 26 | + def __init__(self, start, end): |
| 27 | + self.start = start |
| 28 | + self.end = end |
| 29 | + |
| 30 | +class Solution: |
| 31 | + """ |
| 32 | + @param intervals: an array of meeting time intervals |
| 33 | + @return: if a person could attend all meetings |
| 34 | + """ |
| 35 | + def can_attend_meetings(self, intervals: List[Interval]) -> bool: |
| 36 | + if not intervals: |
| 37 | + return True |
| 38 | + |
| 39 | + intervals.sort(key=lambda x: x.start) |
| 40 | + |
| 41 | + prev_end = intervals[0].end |
| 42 | + |
| 43 | + for interval in intervals[1:]: |
| 44 | + |
| 45 | + if interval.start < prev_end: |
| 46 | + return False |
| 47 | + prev_end = interval.end |
| 48 | + |
| 49 | + return True |
| 50 | + |
0 commit comments