Skip to content

Commit 02cbe89

Browse files
author
Chris Wu
committed
no message
1 parent 2a00678 commit 02cbe89

6 files changed

+237
-22
lines changed

problems/find-minimum-in-rotated-sorted-array.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,42 @@ def findMin(self, nums):
3434
else:
3535
break
3636
return m
37+
38+
39+
40+
"""
41+
Pointer l and r is at the start and at the end of the list. [0]
42+
We only consider the numbers between l and r.
43+
44+
If the list is already sorted, return the left most element. [1]
45+
46+
Now, we cut the rotated array into half. l~m and m~r.
47+
Normally, one part will be sorted, the other half is not.
48+
The min must be in the unsorted part.
49+
So we move the pointer and do the same thing on the unsorted part. [2]
50+
51+
One scenario to consider is that the cutting point, m, being the smallest element.
52+
This will cause both l~m and m~r to both be sorted.
53+
So we need to check it. [3]
54+
55+
Time: O(LogN)
56+
Space: O(1)
57+
"""
58+
59+
class Solution(object):
60+
def findMin(self, nums):
61+
if not nums: return 0
62+
l = 0 #[0]
63+
r = len(nums)-1 #[0]
64+
65+
while l<=r:
66+
if nums[l]<=nums[r]: return nums[l] #[1]
67+
68+
m = (l+r)/2
69+
if m-1>=0 and nums[m-1]>nums[m]: return nums[m] #[3]
70+
71+
if nums[l]<=nums[m]:
72+
l = m+1 #[2]
73+
else:
74+
r = m-1 #[2]
75+
return 0

problems/find-peak-element.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,29 @@ def findPeakElement(self, nums):
3030
r = nums[i+1] if (i+1)<len(nums) else float('-inf')
3131
if nums[i]>l and nums[i]>r: return i
3232
return None
33+
34+
35+
#2020/7/23
36+
class Solution(object):
37+
def findPeakElement(self, nums):
38+
l = 0
39+
r = len(nums)-1
40+
41+
while l<=r:
42+
if l==r: return l
43+
if nums[l]>nums[l+1]: return l #check l is peak
44+
if nums[r-1]<nums[r]: return r #check r is peak
45+
46+
m = (l+r)/2
47+
48+
if nums[m]<nums[m+1]:
49+
#m is in the falling slope
50+
l = m
51+
elif nums[m]>nums[m+1]:
52+
#m is in the rising slope
53+
r = m
54+
return l
55+
56+
57+
58+

problems/search-in-rotated-sorted-array-ii.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,38 @@ def search(self, nums, t):
3535
r = r-1
3636
l = l+1
3737
return False
38+
39+
40+
#2020/7/20, recursive.
41+
class Solution(object):
42+
def search(self, nums, target):
43+
def binary_search(l, r):
44+
if l>r: return False
45+
if nums[l]==target: return True
46+
if nums[r]==target: return True
47+
48+
m = (l+r)/2
49+
if nums[m]==target:
50+
return m
51+
if target<nums[m]:
52+
return binary_search(l, m-1)
53+
else:
54+
return binary_search(m+1, r)
55+
56+
if not nums: return False
57+
58+
def helper(l, r):
59+
if l>r: return False
60+
if nums[l]==target: return True
61+
if nums[r]==target: return True
62+
if nums[l]<nums[r]: return binary_search(l+1, r-1)
63+
64+
m = (l+r)/2
65+
if nums[m]==target: return m
66+
67+
if helper(l+1, m-1): return True
68+
if helper(m+1, r-1): return True
69+
return False
70+
71+
if not nums: return False
72+
return helper(0, len(nums)-1)

problems/search-in-rotated-sorted-array.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,46 @@ def search(self, nums, target):
131131

132132

133133

134+
135+
136+
137+
#2020/7/20, recursive.
138+
class Solution(object):
139+
def search(self, nums, target):
140+
def binary_search(l, r):
141+
if l>r: return -1
142+
if nums[l]==target: return l
143+
if nums[r]==target: return r
144+
145+
m = (l+r)/2
146+
if nums[m]==target:
147+
return m
148+
if target<nums[m]:
149+
return binary_search(l, m-1)
150+
else:
151+
return binary_search(m+1, r)
152+
153+
if not nums: return -1
154+
155+
def helper(l, r):
156+
if l>r: return -1
157+
if nums[l]==target: return l
158+
if nums[r]==target: return r
159+
if nums[l]<=nums[r]: return binary_search(l+1, r-1)
160+
161+
m = (l+r)/2
162+
if nums[m]==target: return m
163+
164+
if nums[l]<nums[m]:
165+
if nums[l]<target and target<nums[m]:
166+
return binary_search(l+1, m-1)
167+
else:
168+
return helper(m+1, r)
169+
else:
170+
if nums[m]<target and target<nums[r]:
171+
return binary_search(m+1, r-1)
172+
else:
173+
return helper(l, m-1)
174+
175+
if not nums: return -1
176+
return helper(0, len(nums)-1)

problems/sqrtx.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,28 @@ def isAns(a):
2525
else:
2626
r = m-1
2727
return -1
28+
29+
30+
"""
31+
The answer must always be in l~r.
32+
In every iteration, we test the m, `m_sqr==x or (m_sqr<x and x<(m+1)**2)`.
33+
If not the answer, we adjust l or r.
34+
35+
Time complexity: O(LogN).
36+
Space complexity: O(1).
37+
"""
38+
class Solution(object):
39+
def mySqrt(self, x):
40+
l = 0
41+
r = x
42+
43+
while l<=r:
44+
m = (l+r)/2
45+
m_sqr = m**2
46+
47+
if m_sqr==x or (m_sqr<x and x<(m+1)**2):
48+
return m
49+
elif m_sqr<x:
50+
l = m+1
51+
else:
52+
r = m-1

problems/time-based-key-value-store.py

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
"""
2-
We need a hash-table to store the key-value information.
3-
For every key, there are multiple values and we need it to sort by`timestamp`.
4-
5-
First, we use two array, `t` and `v` to store timestamp and value separately.
6-
And becuase the problem says **The timestamps for all TimeMap.set operations are strictly increasing.**
7-
So for the `set()` method, we only need to simply append `value` and `timestamp` to the end of `v` and `t`.
8-
The array `t` and its corresponding `v` is sorted already.
9-
10-
[bisect](https://docs.python.org/2/library/bisect.html) can help us implement our `get()` method.
11-
It returns an insertion point which comes after (to the right of) any existing entries, if we insert the **`timestamp` we are going to get()** into a the array, `t`.
12-
So `bisect(t, timestamp)` returns `i`:
13-
* If `i==0`: it means that there are no value samller than the `timestamp`, `return ""`.
14-
* If `i>0`: it means `i` is the index where its value just exceed the `timestamp`, return the value at `i-1`.
15-
16-
We can also implement our own `bisect` by binary search.
17-
The implementation assume there are no duplicate value in the array.
18-
19-
Time Complexity, O(1) for `set()`. O(LogN) for `get()`.
20-
Space complexity is O(N)
21-
N is the number of value stored.
22-
"""
231
import collections
242
import bisect
253

@@ -74,6 +52,29 @@ def bisect(self, A, x):
7452
l = p+1
7553
return l
7654

55+
"""
56+
We need a hash-table to store the key-value information.
57+
For every key, there are multiple values and we need it to sort by`timestamp`.
58+
59+
First, we use two array, `t` and `v` to store timestamp and value separately.
60+
And becuase the problem says **The timestamps for all TimeMap.set operations are strictly increasing.**
61+
So for the `set()` method, we only need to simply append `value` and `timestamp` to the end of `v` and `t`.
62+
The array `t` and its corresponding `v` is sorted already.
63+
64+
[bisect](https://docs.python.org/2/library/bisect.html) can help us implement our `get()` method.
65+
It returns an insertion point which comes after (to the right of) any existing entries, if we insert the **`timestamp` we are going to get()** into a the array, `t`.
66+
So `bisect(t, timestamp)` returns `i`:
67+
* If `i==0`: it means that there are no value samller than the `timestamp`, `return ""`.
68+
* If `i>0`: it means `i` is the index where its value just exceed the `timestamp`, return the value at `i-1`.
69+
70+
We can also implement our own `bisect` by binary search.
71+
The implementation assume there are no duplicate value in the array.
72+
73+
Time Complexity, O(1) for `set()`. O(LogN) for `get()`.
74+
Space complexity is O(N)
75+
N is the number of value stored.
76+
"""
77+
7778
t = TimeMap()
7879
t.set('love', 'low', 10)
7980
t.set('love', 'high', 20)
@@ -85,3 +86,49 @@ def bisect(self, A, x):
8586

8687

8788

89+
#2020/7/20
90+
from collections import defaultdict
91+
class TimeMap(object):
92+
93+
def __init__(self):
94+
self.v = defaultdict(list)
95+
self.t = defaultdict(list)
96+
97+
98+
def set(self, key, value, timestamp):
99+
self.v[key].append(value)
100+
self.t[key].append(timestamp)
101+
102+
103+
def get(self, key, timestamp):
104+
if key not in self.t: return ""
105+
i = self.bisect(self.t[key], timestamp)
106+
return self.v[key][i-1] if i>0 else ""
107+
108+
109+
#return an insertion point right to an index
110+
#where the target's value just exceed the index's value
111+
def bisect(self, L, target):
112+
if not L: return 0
113+
114+
l = 0
115+
r = len(L)-1
116+
117+
while l<=r:
118+
if target<L[l]: return l
119+
if target==L[l]: return l+1
120+
if target>=L[r]: return r+1
121+
122+
m = (l+r)/2
123+
124+
if target==L[m]:
125+
return m+1
126+
elif target<L[m]:
127+
r = m-1
128+
else:
129+
l = m+1
130+
return 0
131+
"""
132+
Time Complexity: set(), O(1). get(), O(LogN).
133+
Space Complexity: O(N).
134+
"""

0 commit comments

Comments
 (0)