defgetFactors(self, n: int) -> List[List[int]]: res = [] self.dfs(n, 2, n, [], res) return res
defdfs(self, n, start, target, path, res): if target == 1: return for i inrange(start, int(math.sqrt(n) + 1)): # remember to use start if target % i != 0or target // i < i: # remember continue path.append(i) res.append(list(path + [target // i])) self.dfs(n, i, target // i, path, res) path.pop()
算法分析:
时间复杂度为<code>O(2<sup>n</sup>)</code>,空间复杂度O(# of prime factors)
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
Implement the MyCalendar class:
MyCalendar() Initializes the calendar object.
boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
Explanation
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.</pre>
Constraints:
0 <= start < end <= 10<sup>9</sup>
At most 1000 calls will be made to book.
</div>
暴力法算法思路(推荐):
存储所有区间
注意事项:
区间比较难写,用符合加入会议条件的相反来写,not (start >= root.end or end <= root.start)
Python代码:
1 2 3 4 5 6 7 8 9 10 11
classMyCalendar(TestCases):
def__init__(self): self.calendar = []
defbook(self, start: int, end: int) -> bool: for s, e inself.calendar: ifnot (start >= e or end <= s): returnFalse self.calendar.append((start, end)) returnTrue
难点在于比较结果决定返回Ture or False。两种情况返回False:好的情况是输入区间的搜索结果的下标应该相等表示可以插入到现有两区间之间,所以若不等,就不符合。第二种是若现有区间完全覆盖新区间,此时搜索结果下标也一样,所以此情况新区间终点的搜索结果(后一个节点)是终点。同时为保证搜索结果不处理越界,加入空端点
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
Implement the MyCalendarTwo class:
MyCalendarTwo() Initializes the calendar object.
boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event ca not be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
</pre>
Constraints:
0 <= start < end <= 10<sup>9</sup>
At most 1000 calls will be made to book.
</div>
暴力法算法思路(推荐):
存储所有区间和重合区间
注意事项:
区间比较难写,用符合加入会议条件的相反来写,not (start >= root.end or end <= root.start)
defbook(self, start: int, end: int) -> bool: for s, e inself.overlaps: ifnot (start >= e or end <= s): returnFalse
for s, e inself.calendar: ifnot (start >= e or end <= s): self.overlaps.append((max(start, s), min(end, e))) self.calendar.append((start, end)) returnTrue
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).