zoukankan      html  css  js  c++  java
  • 252. Meeting Rooms

    https://leetcode.com/problems/meeting-rooms/#/description

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

    For example,
    Given [[0, 30],[5, 10],[15, 20]],
    return false.

    Sol:

    The end time of the previous interval should be less or equal than the start time of the start time of the next interval....But wait, sort out first! by ascending order of the start time. 

    # Definition for an interval.
    class Interval(object):
        def __init__(self, s=0, e=0):
            self.start = s
            self.end = e
    
    class Solution(object):
        def canAttendMeetings(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: bool
            """
            if len(intervals) < 2:
                return True
            intervals = sorted(intervals, key = lambda x: x.start)
            for i in range(len(intervals)-1):
                if intervals[i].end > intervals[i+1].start:
                    return False
            return True
            
            

    Note:

    1 The input list is composed of tuples. 

    ex. s = [ [11,22], [33, 44] ]

    len(s) = 2

    2 The problem pre-defines the class of the tuple object.  Tuples are [s, e] and they are defined as:

    self.start = s

    self.end = e

    So when calling the first element of the tuple, we should use intervals.start.  

    3 Edge Check! 

    4 One way to sort by certain element of a turple. 

    intervals = sorted(intervals, key = lambda x: x.start)

    1) sorted, key = lambda are the build-in python chars. x can be any name.

    2) The sentence above means sort intervals by the start/first element.

    3)Two more examples:

    ex1.

    s = [['b', 2], ['a', 1], ['c', 0]]

    s = sorted(s, key = lambda any_name: any_name[0])

    print s

    ==>  [['a', 1], ['b', 2], ['c', 0]]

    This is sorted by the first element of the tuple. 

    ex2. 

    s = [['b', 2], ['a', 1], ['c', 0]]

    s = sorted(s, key = lambda any_name: any_name[1])

    print s

    ==>  [['c', 0], ['a', 1], ['b', 2]]

    This is sorted by the second element of the tuple. 

     5 Pay attention to the end condition of the index in for loop, especially the loop contains index + 1 or index - 1.  Otherwise it will get  "Line 17: IndexError: list index out of range".

  • 相关阅读:
    linux下使用c99链接libuv遇到的问题
    linux连接lua遇到的问题
    uv_timer_t的释放问题
    libuv的源码分析(1)
    [转]关于截取字符串substr和substring两者的区别
    输入框获取焦点后placeholder文字消失、修改placeholder的样式
    发送验证码功能
    针对移动浏览器判断不同的内核的方法
    preventDefault()对象
    [转]JQ中$(window).load和$(document).ready区别与执行顺序
  • 原文地址:https://www.cnblogs.com/prmlab/p/6950218.html
Copyright © 2011-2022 走看看