Skip to content

Commit 61a2cde

Browse files
committed
add a few easy ones.
1 parent bc0bfcd commit 61a2cde

File tree

6 files changed

+99
-21
lines changed

6 files changed

+99
-21
lines changed

LinkedList.pyc

1.18 KB
Binary file not shown.

median_two_arrays.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def median_two_arrays(A, B):
3737

3838
### use binary search
3939
# http://leetcode.com/2011/03/median-of-two-sorted-arrays.html
40+
# KEY: #elements being disposed from each array must be the same.
4041
# ~O(logn + logm)
4142
def median_two_arrays2(A, B):
4243
#print A, B, '--->',
@@ -46,13 +47,18 @@ def median_two_arrays2(A, B):
4647
i = m//2
4748
j = n//2
4849
if A[i] <= B[j]:
49-
# median is between [A[i], B[j]]
50+
# Median is between [A[i], B[j]], so
51+
# we can ignore A's left part, A[0:i], length = i
52+
# we can ignore B's right part, B[j+1:n], length = n-j-1
5053
l, r = i, n-j-1
51-
k = min(l,r)+1 if l != r else min(l,r) # THIS IS TRICKY!
54+
### THIS IS TRICKY!
55+
# '+1' helps include the medians in A & B.
56+
# when l == r, we can safely ignore current medians.
57+
k = min(l,r)+1 if l != r else min(l,r)
5258
A = A[k:]
5359
B = B[:-k]
5460
elif A[i] > B[j]:
55-
# median is between [B[j], A[i]]
61+
# Median is between [B[j], A[i]]
5662
l,r = j, m-i-1
5763
k = min(l,r)+1 if l != r else min(l,r)
5864
A = A[:-k]
@@ -73,7 +79,9 @@ def median_one_array(A):
7379

7480

7581
##########################################################
76-
82+
'''
83+
Find kth smallest in two sorted arrays.
84+
'''
7785
def kth_two_arrays(A, B, k):
7886
k -= 1
7987
m, n = len(A), len(B)

multiplication_of_nums.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
'''
3+
Leetcode: There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. Solve it without division operator and in O(n).
4+
5+
For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].
6+
http://leetcode.com/2010/04/multiplication-of-numbers.html
7+
'''
8+
from __future__ import division
9+
import random
10+
11+
12+
def multi_of_nums(A):
13+
n = len(A)
14+
L = [1]*n
15+
R = [1]*n
16+
for i in range(n-1):
17+
L[i+1] = L[i]*A[i]
18+
for i in range(n-1,0,-1):
19+
R[i-1] = R[i]*A[i]
20+
ret = [L[i]*R[i] for i in range(n)]
21+
print ret
22+
return ret
23+
24+
25+
if __name__ == '__main__':
26+
multi_of_nums([4,3,2,1,2,3])
27+
28+

partition_list.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,30 @@
1212
from LinkedList import Node
1313

1414

15-
def part_list(node, val):
16-
root = node
17-
small = None # the end of "small part" of the link
18-
large = None # the end of "large part" of the link
15+
def part_list(head, val):
16+
dummy = Node(None, next=head)
17+
node = head
18+
small = dummy # the end of "small part" of the link
19+
large = dummy # the end of "large part" of the link
1920
while node:
20-
print 'current node', node.value
21+
print head, 'small:', small.value, 'large:', large.value,
22+
print 'current:', node, 'next:', node.next
2123
next_node = node.next
2224
if node.value >= val:
23-
# the first large element
2425
large = node
2526
else:
26-
if large:
27+
# Change the connection
28+
if small.next != node:
2729
print 'move', node.value
28-
# change the connection
29-
if small:
30-
node.next = small.next
31-
small.next = node
32-
else: # change root
33-
node.next = root
34-
root = node
30+
node.next = small.next
31+
small.next = node
3532
large.next = next_node
3633
# update the tail pointer to the small part
3734
small = node
3835
node = next_node
39-
print root
40-
return root
36+
head = dummy.next
37+
print head
38+
return head
4139

4240

4341
if __name__ == '__main__':

prime_num.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import division
4+
import random
5+
from math import sqrt
6+
7+
'''
8+
Output all prime numbers up to a specified integer n.
9+
'''
10+
# using The Sieve of Erantosthenes
11+
def prime_sieve(n):
12+
is_prime = dict((i,True) for i in range(2,n+1))
13+
limit = int(sqrt(n))
14+
for i in range(2, limit+1):
15+
for j in range(2, n//i+1):
16+
is_prime[i*j] = False
17+
ret = [n for n in is_prime if is_prime[n]]
18+
print ret
19+
return ret
20+
21+
'''
22+
Output first n prime numbers
23+
'''
24+
def prime(n):
25+
primes = [2]
26+
num = 3
27+
while len(primes) < n:
28+
is_prime = True
29+
for p in primes:
30+
if num % p == 0:
31+
is_prime = False
32+
break
33+
if is_prime:
34+
primes.append(num)
35+
num += 1
36+
print primes
37+
return primes
38+
39+
40+
if __name__ == '__main__':
41+
prime_sieve(230)
42+
prime(50)
43+
44+

triangle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# S[i][j] = min{S[i-1][j-1], S[i-1][j]} + T[i][j]
1818
# We only need to keep the record of the last row
19-
# S[j] = min{S[j-1], S[j]} + T[i][j]
19+
# S[j] = min{S[j-1], S[j]} + T[i][j]}
2020
# top-down
2121
def triangle(T):
2222
if not T: return 0

0 commit comments

Comments
 (0)