Fancy Python solution


  • 1
    S
    class Solution(object):
        def canAttendMeetings(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: bool
            """
            a = []
            map(a.extend, sorted((i.start, i.end) for i in intervals))
            return a == sorted(a)

  • 0

    Great idea, and neat (ab)use of map.

    Translated to Ruby, which has flatten:

    def can_attend_meetings(intervals)
      a = intervals.map { |i| [i.start, i.end] }.sort.flatten
      a == a.sort
    end
    

    Even works as one-liner (a = ...) == a.sort, though I'm not sure it's safe (Ruby newb here).


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.