Skip to content

Commit fac3abc

Browse files
committed
no message
1 parent 0c70368 commit fac3abc

File tree

6 files changed

+160
-1
lines changed

6 files changed

+160
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Time: O(kLogk)
3+
Space: O(N)
4+
5+
[0]
6+
Declare a min-heap. Which stores the (sum, index in nums1, index in nums2) for each combination.
7+
8+
[1]
9+
Each iteration, we get a min sum (i, j) from the heap and add it to the `ans`.
10+
The next possible smallest combination will be either (i+1, j) or (j+1, i) or other combination previously added.
11+
Add combination (i+1, j) and (j+1, i) to the heap.
12+
13+
[2]
14+
Use a set `seen` to avoid adding the same element to the heap.
15+
For example (i+1, j) and (j+1, i) can both lead to (i+1, j+1).
16+
"""
17+
class Solution(object):
18+
def kSmallestPairs(self, nums1, nums2, k):
19+
if not nums1 or not nums2: return []
20+
if k>len(nums1)*len(nums2): k = len(nums1)*len(nums2)
21+
22+
ans = []
23+
h = [(nums1[0]+nums2[0], 0, 0)] #[0]
24+
seen = set([(0,0)]) #[2]
25+
26+
while len(ans)<k: #[1]
27+
s, i, j = heapq.heappop(h)
28+
ans.append((nums1[i], nums2[j]))
29+
30+
if i+1<len(nums1) and (i+1, j) not in seen:
31+
heapq.heappush(h, (nums1[i+1]+nums2[j], i+1, j))
32+
seen.add((i+1, j))
33+
34+
if j+1<len(nums2) and (i, j+1) not in seen:
35+
heapq.heappush(h, (nums1[i]+nums2[j+1], i, j+1))
36+
seen.add((i, j+1))
37+
38+
return ans

problems/find-the-duplicate-number.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Time: O(N)
3+
Space: O(1)
4+
5+
Floyd's Tortoise and Hare Algorithm: Given a linked list, return the node where the cycle begins.
6+
"""
7+
class Solution(object):
8+
def findDuplicate(self, nums):
9+
10+
#find the intersect node, where slow and fast pointer meets.
11+
slow = fast = 0
12+
while True:
13+
slow = nums[slow]
14+
fast = nums[nums[fast]]
15+
if slow==fast: break
16+
17+
#find the intersect node, where slow and slow2 pointer meets. slow2 is starting from the begin.
18+
slow2 = 0
19+
while True:
20+
slow = nums[slow]
21+
slow2 = nums[slow2]
22+
if slow==slow2: return slow
23+
24+
return 0

problems/missing-number.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Time: O(N)
3+
Space: O(N)
4+
"""
5+
class Solution(object):
6+
def missingNumber(self, nums):
7+
maxNum = max(nums)
8+
s = set(nums)
9+
10+
for num in xrange(maxNum+1):
11+
if num not in s:
12+
return num
13+
14+
return maxNum+1
15+
16+
"""
17+
Time: O(N)
18+
Space: O(1)
19+
20+
[0] Gauss' Formula: 0+1+2+...+n = n*(n+1)/2
21+
[1] Knowing that there is a missing num in nums, the last/max num will be len(nums).
22+
"""
23+
class Solution:
24+
def missingNumber(self, nums):
25+
n = len(nums) #[1]
26+
expectedSum = n*(n+1)/2 #[0]
27+
actualSum = sum(nums)
28+
return expectedSum-actualSum

problems/reverse-words-in-a-string.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ def reverseWords(self, s):
2424
temp+=c
2525

2626
opt = opt[:-1] #[2]
27-
return opt
27+
return opt
28+
29+
30+
31+
class Solution(object):
32+
def reverseWords(self, s):
33+
stack = s.split()
34+
35+
ans = ''
36+
while stack: ans += stack.pop()+' '
37+
ans = ans[:len(ans)-1] #remove last space
38+
return ans

problems/rotate-array.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Time: O(N)
3+
Space: O(k)
4+
"""
5+
class Solution(object):
6+
def rotate(self, nums, k):
7+
if len(nums)<=1: return nums
8+
9+
N = len(nums)
10+
k = k%N
11+
store = nums[:k]
12+
13+
for i in xrange(N-1, k-1, -1):
14+
j = i+k if i+k<N else i+k-N
15+
nums[j] = nums[i]
16+
17+
for i in xrange(len(store)):
18+
j = i+k if i+k<N else i+k-N
19+
nums[j] = store[i]
20+
21+
"""
22+
Time: O(N)
23+
Space: O(1)
24+
"""
25+
class Solution(object):
26+
def rotate(self, nums, k):
27+
def reverse(A, start, end):
28+
while start<end:
29+
A[start], A[end] = A[end], A[start]
30+
start += 1
31+
end -= 1
32+
33+
34+
N = len(nums)
35+
k = k%N
36+
reverse(nums, 0, N-1-k)
37+
reverse(nums, N-k, N-1)
38+
reverse(nums, 0, N-1)

problems/super-ugly-number.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def nthSuperUglyNumber(self, n, primes):
3+
p = [0]*len(primes) #p[i] stores the index of ugly number in ans that not yet times primes[i] yet
4+
ans = [1]
5+
h = []
6+
7+
for i in xrange(len(primes)):
8+
heapq.heappush(h, (primes[i]*ans[p[i]], i))
9+
10+
for _ in xrange(n-1):
11+
curr = h[0][0]
12+
ans.append(curr)
13+
14+
while h and h[0][0]==curr:
15+
i = h[0][1]
16+
heapq.heappop(h)
17+
p[i] += 1
18+
heapq.heappush(h, (primes[i]*ans[p[i]], i))
19+
20+
return ans[-1]

0 commit comments

Comments
 (0)