題目出處
難度
easy
個人範例程式碼
from typing import (
List,
)
from lintcode import (
Interval,
)
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
"""
@param intervals: an array of meeting time intervals
@return: if a person could attend all meetings
"""
def can_attend_meetings(self, intervals: List[Interval]) -> bool:
# Write your code here
if not intervals:
return True
intervals.sort(key = lambda x:x.start)
end = intervals[0].end
for i, interval in enumerate(intervals):
if i > 0:
if end <= interval.start: # end <= next_start (0,8), (8,10)
end = interval.end
continue
else:
return False
return True
算法說明
單純檢查會議室有沒有重疊時間的問題,我們可以先 sort start 時間後,
再檢查是否「上一個 end <= 下一個 start 」的時間點
(題目有說,(0, 8),(8, 10)不算是共用會議室,因此要有「等於」)
當我們發現不符合我們上述的規則時,
直接 return False。
input handling
如果沒有 intervals, 回傳 True (題目沒特別要求)
Boundary conditions
用 for 迴圈控制範圍
Reference
⭐ Leetcode 解題紀錄 ⭐ | 題型 | 資料結構 | Python Solution | C++ Solution | Note | |
---|---|---|---|---|---|---|
⭐BFS 相關題型 ⭐ | ||||||
104 | Maximum Depth of Binary Tree | BFS (分層) | Python | |||
94 | Binary Tree Inorder Traversal | BFS (分層) | Tree | Python | 內含 處理 Tree 樹問題的重點 | |
102 | Binary Tree Level Order Traversal | BFS (分層) | Tree | Python | ||
103 | Binary Tree Zigzag Level Order Traversal | BFS (分層) | Tree | Python | ||
107 | Binary Tree Level Order Traversal II | BFS (分層) | Tree | Python | ||
133 | Clone Graph | BFS (分層) | Graph | Python | Graph 的基本操作 #重要題型 | |
127 | Word Ladder | BFS (分層), DFS | Graph | Python | ||
[Lint] 127 | Topological Sorting | BFS (拓撲) | Python | 內有 indegree, outdegree 介紹 #重要題型 | ||
207 | Course Schedule | BFS (拓樸) | Graph | Python | ||
210 | Course Schedule II | BFS (拓樸) | Graph | Python | ||
[Lint] 892 | Alien Dictionary | BFS (拓樸) | Graph | Python | ||
[Lint] 431 | Connected Component in Undirected Graph | BFS (連通塊) | Graph | Python | 內含 BFS 模板 #重要題型 | |
1091 | Shortest Path in Binary Matrix | BFS (最短路徑) | Matrix | Python | ||
⭐ Binary Serach 相關題型 ⭐ | ||||||
33 |