Skip to content

Commit bd75bd1

Browse files
committed
255
1 parent 9a3d356 commit bd75bd1

File tree

8 files changed

+169
-0
lines changed

8 files changed

+169
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for an interval.
2+
# class Interval(object):
3+
# def __init__(self, s=0, e=0):
4+
# self.start = s
5+
# self.end = e
6+
import operator
7+
class Solution(object):
8+
def minMeetingRooms(self, intervals):
9+
"""
10+
:type intervals: List[Interval]
11+
:rtype: int
12+
"""
13+
if not intervals:
14+
return 0
15+
pairs = []
16+
for interval in intervals:
17+
pairs.append((interval.start, 1))
18+
pairs.append((interval.end, -1))
19+
# sort first by time value, and make sure endtime (-1) comes before starttime(1)
20+
pairs.sort()
21+
22+
23+
max_depth = 0
24+
depth = 0
25+
for _, pos in pairs:
26+
if pos>0:
27+
depth += 1
28+
if depth > max_depth:
29+
max_depth = depth
30+
else:
31+
depth -= 1
32+
return max_depth

253_meeting_room_ii/problem.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
2+
3+
For example,
4+
Given [[0, 30],[5, 10],[15, 20]],
5+
return 2.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import math
2+
class Solution(object):
3+
def getFactors(self, n):
4+
"""
5+
:type n: int
6+
:rtype: List[List[int]]
7+
"""
8+
self.memo = {}
9+
return self.recurse(n)
10+
11+
def recurse(self, n):
12+
if n in self.memo:
13+
return self.memo[n]
14+
result = []
15+
sqrtn = int(math.sqrt(n))+1
16+
for i in range(2, sqrtn):
17+
if n%i==0:
18+
result.append([i, n/i])
19+
sub = self.recurse(n/i)
20+
for lst in sub:
21+
if lst[0]>=i:
22+
result.append([i]+lst)
23+
self.memo[n] = result
24+
return result
25+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Numbers can be regarded as product of its factors. For example,
2+
3+
8 = 2 x 2 x 2;
4+
= 2 x 4.
5+
6+
Note:
7+
Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is [2, 6], not [6, 2].
8+
You may assume that n is always positive.
9+
Factors should be greater than 1 and less than n.
10+
Examples:
11+
input: 1
12+
output:
13+
[]
14+
input: 37
15+
output:
16+
[]
17+
input: 12
18+
output:
19+
[
20+
[2, 6],
21+
[2, 2, 3],
22+
[3, 4]
23+
]
24+
input: 32
25+
output:
26+
[
27+
[2, 16],
28+
[2, 2, 8],
29+
[2, 2, 2, 4],
30+
[2, 2, 2, 2, 2],
31+
[2, 4, 4],
32+
[4, 8]
33+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
2+
3+
You may assume each number in the sequence is unique.
4+
5+
Follow up:
6+
Could you do it using only constant space complexity?
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def verifyPreorder(self, preorder):
3+
stack = []
4+
lower = -1 << 31
5+
for x in preorder:
6+
if x < lower:
7+
return False
8+
while stack and x > stack[-1]:
9+
lower = stack.pop()
10+
stack.append(x)
11+
return True
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def verifyPreorder(self, preorder):
3+
"""
4+
:type preorder: List[int]
5+
:rtype: bool
6+
"""
7+
if len(preorder)<2:
8+
return True
9+
root = preorder[0]
10+
breakpoint = -1
11+
for i in range(1, len(preorder)):
12+
if preorder[i]>root:
13+
if breakpoint<0:
14+
breakpoint = i
15+
if preorder[i]<root and breakpoint>0:
16+
return False
17+
if breakpoint <0:
18+
breakpoint = len(preorder)
19+
return self.verifyPreorder(preorder[1:breakpoint]) and self.verifyPreorder(preorder[breakpoint:])
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
def verifyPreorder(self, preorder):
3+
"""
4+
:type preorder: List[int]
5+
:rtype: bool
6+
"""
7+
if len(preorder)<2:
8+
return True
9+
roots = [] # contains tuples (node, direction) where direction 0 for left and 1 for right
10+
current = preorder[0]
11+
for num in preorder[1:]:
12+
if num < current:
13+
for i in range(len(roots)-1, -1, -1):
14+
val, dir = roots[i]
15+
if dir==1: # right branch
16+
if num < val:
17+
return False
18+
else:
19+
break
20+
roots.append((current, 0))
21+
current = num
22+
else:
23+
top = -1
24+
for i in range(len(roots)-1, -1, -1):
25+
val, dir = roots[i]
26+
if dir==0:
27+
if num < val:
28+
break
29+
else:
30+
top = i
31+
if top < 0:
32+
roots.append((current, 1))
33+
else:
34+
del roots[top+1:]
35+
roots[top] = (roots[top][0], 1)
36+
current = num
37+
return True
38+

0 commit comments

Comments
 (0)