Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 872 Bytes

252.md

File metadata and controls

38 lines (33 loc) · 872 Bytes

对二层进行排序,实际上没必要

  • 76ms
  • 93%
  • time: O(nlgn)
  • space: O(1)
from functools import cmp_to_key

def cmp(a, b):
    if a[0]==b[0]:
        return [-1, 1][a[1]>b[1]]
    return [-1, 1][a[0]>b[0]]

class Solution:
    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
        intervals.sort(key=cmp_to_key(cmp))
        for i in range(1, len(intervals)):
            if intervals[i][0] < intervals[i-1][1]:
                return False
        return True

仅对开始时间进行排序

  • 40ms
  • 98%
  • time: O(nlgn)
  • space: O(1)
class Solution:
    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
        intervals.sort(key=lambda x: x[0])
        for i in range(1, len(intervals)):
            if intervals[i][0] < intervals[i-1][1]:
                return False
        return True