当前位置:网站首页>Leetcode medium question my schedule I

Leetcode medium question my schedule I

2022-07-07 08:09:00 ·Starry Sea

subject

Achieve one MyCalendar Class to store your schedule . If the schedule to be added does not cause Repeat Booking , You can store this new schedule .

When there is some time overlap between the two schedules ( For example, both schedules are in the same time ), It will produce Repeat Booking .

The schedule can use a pair of integers start and end Express , The time here is a half open interval , namely [start, end), The set of real Numbers x For the range of , start <= x < end .

Realization MyCalendar class :

MyCalendar() Initialize calendar object .
boolean book(int start, int end) If the schedule can be successfully added to the calendar without causing duplicate bookings , return true . otherwise , return false And don't add the schedule to the calendar .

Example :

Input :
[“MyCalendar”, “book”, “book”, “book”]
[[], [10, 20], [15, 25], [20, 30]]
Output :
[null, true, false, true]

explain :
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False , This schedule cannot be added to the calendar , Because of time 15 Has been booked by another schedule .
myCalendar.book(20, 30); // return True , This schedule can be added to the calendar , Because the first schedule is booked at less than each time 20 , And does not include time 20 .

Tips :

0 <= start < end <= 10^9
Each test case , call book The maximum number of methods is 1000 Time .

source : Power button (LeetCode)

Their thinking

   Record it in the simplest way .

class MyCalendar:

    def __init__(self):
        self.record=[]

    def book(self, start: int, end: int) -> bool:
        if not self.record:
            self.record.append([start,end])
            return True
        else:
            for i in self.record:
                if i[0]<end and i[1]>start:
                    return False
            self.record.append([start,end])
            return True


# Your MyCalendar object will be instantiated and called as such:
# obj = MyCalendar()
# param_1 = obj.book(start,end)

 Insert picture description here

原网站

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070446576359.html