Skip to content

Commit ecf30bf

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

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution(object):
2+
def shipWithinDays(self, weights, D):
3+
l = max(weights)
4+
r = sum(weights)
5+
6+
while l<r:
7+
c = (l+r)/2
8+
9+
#calculate with weight capacity c, how many days we need, d.
10+
d = 0
11+
daily_weight = 0
12+
for w in weights:
13+
if daily_weight+w<=c:
14+
daily_weight+=w
15+
else:
16+
daily_weight = w
17+
d += 1
18+
if daily_weight: d += 1
19+
20+
if d>D:
21+
#K cannot be the answer.
22+
#next round we don't need to put K in l~r.
23+
l = c+1
24+
else:
25+
#K might ot might not be the answer.
26+
#next round we still need to put K in l~r.
27+
r = c
28+
return l
29+
30+
"""
31+
This is a binary search problem.
32+
If you do not understand binary search yet, please study it first.
33+
34+
[0]
35+
Lets define the possible range, `l` and `r`, of our answer, the least weight capacity of the ship that can diliver all packages within D days.
36+
The ship must at least carry the heaviest package, so `l = max(weights)`.
37+
The best ship scenario is that we can carry all packages at once, so `r = sum(weights)`.
38+
39+
[1]
40+
For every iteration, we try a `c` (capacity) and adjust `l` and `r`.
41+
Note that, even if `d<=D`, we still need to see if there are any smaller `d`.
42+
43+
[2]
44+
So the boundary of our answer, `l` and `r`, will collides together (`l==r`) and jump out of the loop.
45+
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+
"""

problems/koko-eating-bananas.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,49 @@ def canEatAll(time):
2929
else:
3030
l = m+1
3131
return l
32+
33+
34+
#2020/7/23
35+
class Solution(object):
36+
def minEatingSpeed(self, piles, H):
37+
if not piles: return 0
38+
39+
l = 1
40+
r = max(piles) #[0]
41+
42+
while l<r:
43+
K = (l+r)/2 #[1]
44+
45+
#time Koko needs to eat all bananas
46+
t = sum([-(-banana_count//K) for banana_count in piles]) #-(-a//b) means ceil(a/b)
47+
48+
if t>H:
49+
#K cannot be the answer.
50+
#next round we don't need to put K in l~r.
51+
l = K+1
52+
else:
53+
#K might ot might not be the answer.
54+
#next round we still need to put K in l~r.
55+
r = K
56+
57+
return l #[2]
58+
59+
"""
60+
This is a binary search problem.
61+
If you do not understand binary search yet, please study it first.
62+
63+
[0]
64+
Lets define the possible range, `l` and `r`, of our answer, the minimum integer `K` such that Koko can eat all the bananas within `H` hours.
65+
The best scenario is that Koko can eat the whole pile at once.
66+
So `K` must be between 1 ~ `max(piles)`. `l = 1`, `r = max(piles)`.
67+
68+
[1]
69+
For every iteration, we try a `K` and adjust `l` and `r`.
70+
Note that, even if `t<=H`, we still need to see if there are any smaller `K`.
71+
72+
[2]
73+
So the boundary of our answer, `l` and `r`, will collides together (`l==r`) and jump out of the loop.
74+
75+
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`.
76+
Space complexity is O(N). For calculating `t`.
77+
"""

problems/search-a-2d-matrix.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,51 @@ def getMatrix(i):
4747
else:
4848
l = p+1
4949
return False
50+
51+
52+
#2020/7/23
53+
class Solution(object):
54+
def searchMatrix(self, matrix, target):
55+
if not matrix or not matrix[0]: return False
56+
57+
#if you do not understand binary search yet, please study it first.
58+
#use binary search to find the list that has target.
59+
#if found, asign it to A.
60+
l = 0
61+
r = len(matrix)-1
62+
A = None
63+
while l<=r:
64+
if matrix[l][0]<=target and target<=matrix[l][-1]:
65+
A = matrix[l]
66+
break
67+
if matrix[r][0]<=target and target<=matrix[r][-1]:
68+
A = matrix[r]
69+
break
70+
71+
m = (l+r)/2
72+
73+
if matrix[m][0]<=target and target<=matrix[m][-1]:
74+
A = matrix[m]
75+
break
76+
elif target<matrix[m][0]:
77+
r = m-1
78+
else:
79+
l = m+1
80+
81+
if not A: return False
82+
83+
#find if target in A
84+
l = 0
85+
r = len(A)-1
86+
while l<=r:
87+
if target==A[l] or target==A[r]: return True
88+
89+
m = (l+r)/2
90+
91+
if target==A[m]:
92+
return True
93+
elif target<A[m]:
94+
r = m-1
95+
else:
96+
l = m+1
97+
return False

0 commit comments

Comments
 (0)