Skip to content

Commit cda3f41

Browse files
author
Chris Wu
committed
no message
1 parent ecf30bf commit cda3f41

7 files changed

+176
-16
lines changed

problems/capacity-to-ship-packages-within-d-days.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ def shipWithinDays(self, weights, D):
1818
if daily_weight: d += 1
1919

2020
if d>D:
21-
#K cannot be the answer.
22-
#next round we don't need to put K in l~r.
21+
#c cannot be the answer.
22+
#next round we don't need to put c in l~r.
2323
l = c+1
2424
else:
25-
#K might ot might not be the answer.
26-
#next round we still need to put K in l~r.
25+
#c might ot might not be the answer.
26+
#next round we still need to put c in l~r.
2727
r = c
2828
return l
2929

30+
31+
3032
"""
3133
This is a binary search problem.
3234
If you do not understand binary search yet, please study it first.
@@ -43,6 +45,12 @@ def shipWithinDays(self, weights, D):
4345
[2]
4446
So the boundary of our answer, `l` and `r`, will collides together (`l==r`) and jump out of the loop.
4547
46-
Time complexity: `O(NlogN)`. There will be `O(LogN)` iteration. For every iteration we need O(N) to calculate the `t`. `N` is the length of `piles`.
47-
Space complexity is O(N). For calculating `t`.
48+
Time complexity: `O(NlogW)`.
49+
`N` is the number of `weights`.
50+
`W` is the max weight.
51+
There will be `O(LogW)` iteration. For every iteration we need O(N) to calculate the `d`.
52+
Space complexity is O(1).
53+
54+
Also take a look at problem, 875, very similar.
55+
https://leetcode.com/problems/koko-eating-bananas/discuss/750699/
4856
"""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution(object):
2+
def insertIntoBST(self, root, val):
3+
if not root: return TreeNode(val)
4+
5+
node = root
6+
while node:
7+
if val<node.val:
8+
if not node.left:
9+
node.left = TreeNode(val)
10+
return root
11+
else:
12+
node = node.left
13+
else:
14+
if not node.right:
15+
node.right = TreeNode(val)
16+
return root
17+
else:
18+
node = node.right
19+
20+
return None
21+
22+
23+
"""
24+
Time complexity: O(LogN)
25+
Space complexity: O(1)
26+
"""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def kthSmallest(self, root, k):
3+
count = 0
4+
stack = []
5+
node = root
6+
7+
while node or stack:
8+
while node:
9+
stack.append(node)
10+
node = node.left
11+
12+
node = stack.pop()
13+
count += 1
14+
15+
if count==k: return node.val
16+
17+
node = node.right
18+
19+
return 0
20+
21+
"""
22+
Time complexity: O(N). Because we use inorder traversal.
23+
Space complexity: O(N). For stack may contains all the nodes.
24+
"""
25+
26+
"""
27+
For follow up question.
28+
We might need to keep a sorted list of node.
29+
Every time we inset and delete. We need to update the list. Taking up O(N) of time.
30+
(Originally it was O(LogN) for insert and delete)
31+
But to find the kth smallest element will only takes O(1).
32+
"""

problems/kth-smallest-element-in-a-sorted-matrix.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,38 @@ def kthSmallest(self, matrix, k):
1212
opt.append(m)
1313
memo[i_min]+=1
1414
return opt[-1]
15+
16+
"""
17+
Time: O(kN).
18+
Space: O(kN)
19+
"""
20+
21+
22+
class Solution(object):
23+
def kthSmallest(self, matrix, k):
24+
def count_smaller(t):
25+
count = 0
26+
col = 0
27+
for i, row in enumerate(reversed(matrix)):
28+
while col<len(row) and row[col]<=t:
29+
count += len(matrix)-i
30+
col += 1
31+
return count
32+
33+
l = matrix[0][0]
34+
r = matrix[-1][-1]
35+
36+
while l<r:
37+
m = (l+r)/2
38+
c = count_smaller(m)
39+
40+
if c<k:
41+
l = m+1
42+
else:
43+
r = m
44+
45+
return l
46+
47+
"""
48+
Time: O(NLOG(max-min))
49+
"""
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution(object):
2+
def getMinimumDifference(self, root):
3+
stack = []
4+
node = root
5+
temp = float('-inf')
6+
ans = float('inf')
7+
8+
while node or stack:
9+
while node:
10+
stack.append(node)
11+
node = node.left
12+
13+
node = stack.pop()
14+
ans = min(ans, node.val-temp)
15+
temp = node.val
16+
17+
node = node.right
18+
return ans
19+
20+
"""
21+
Time complexity: O(N)
22+
Space complexity: O(1)
23+
"""
24+
25+
26+
class Solution(object):
27+
def getMinimumDifference(self, root):
28+
def traverse(node):
29+
if not node: return
30+
traverse(node.left)
31+
A.append(node.val)
32+
traverse(node.right)
33+
34+
A = []
35+
ans = float('inf')
36+
traverse(root)
37+
38+
for i, n in enumerate(A):
39+
if i==0: continue
40+
ans = min(ans, A[i]-A[i-1])
41+
return ans
42+
43+
"""
44+
Time complexity: O(N)
45+
Space complexity: O(N)
46+
"""
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def searchBST(self, root, val):
3+
node = root
4+
5+
while node:
6+
if node.val==val:
7+
return node
8+
elif node.val>val:
9+
node = node.left
10+
else:
11+
node = node.right
12+
13+
return None
14+
15+
16+
"""
17+
Time complexity: O(LogN)
18+
Space complexity: O(1)
19+
"""

problems/validate-binary-search-tree.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ class Solution(object):
2929
#recursive
3030
def isValidBST(self, root):
3131
def helper(node, min_val, max_val):
32-
if node==None:
33-
return True
34-
if node.val<=min_val or node.val>=max_val:
35-
return False
36-
37-
left_valid = helper(node.left, min_val, node.val)
38-
if not left_valid: return False
39-
right_valid = helper(node.right, node.val, max_val)
40-
if not right_valid: return False
41-
32+
if not node: return True
33+
if node.val<=min_val or node.val>=max_val:return False
34+
if not helper(node.left, min_val, node.val): return False
35+
if not helper(node.right, node.val, max_val): return False
4236
return True
4337
return helper(root, float('-inf'), float('inf'))
4438

0 commit comments

Comments
 (0)