Skip to content

Commit 0074abb

Browse files
committed
initial commit
0 parents  commit 0074abb

File tree

118 files changed

+1586
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+1586
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
download.py

3Sum Closest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def threeSumClosest(self, A, target):
3+
A, result, closest_diff, i = sorted(A), 2147483647, 2147483647, 0
4+
while i < len(A) - 2:
5+
j, k = i + 1, len(A) - 1
6+
while j < k:
7+
diff = A[i] + A[j] + A[k] - target
8+
if diff < 0:
9+
if math.fabs(diff) < math.fabs(closest_diff):
10+
result, closest_diff = A[i] + A[j] + A[k], diff
11+
j += 1
12+
elif diff > 0:
13+
if math.fabs(diff) < math.fabs(closest_diff):
14+
result, closest_diff = A[i] + A[j] + A[k], diff
15+
k -= 1
16+
else:
17+
return target
18+
i += 1
19+
return result

3Sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def threeSum(self, A):
3+
A, result, i = sorted(A), [], 0
4+
while i < len(A) - 2:
5+
j, k = i + 1, len(A) - 1
6+
while j < k:
7+
if A[i] + A[j] + A[k] < 0:
8+
j += 1
9+
elif A[i] + A[j] + A[k] > 0:
10+
k -= 1
11+
else:
12+
result.append([A[i], A[j], A[k]])
13+
j, k = j + 1, k - 1
14+
while j < k and A[j] == A[j - 1]:
15+
j += 1
16+
while j < k and A[k] == A[k + 1]:
17+
k -= 1
18+
i += 1
19+
while i < len(A) - 2 and A[i] == A[i - 1]:
20+
i += 1
21+
return result

Add Binary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def addBinary(self, a, b):
3+
return bin(int(a, 2) + int(b, 2)).split('b')[1]

Add Two Numbers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def addTwoNumbers(self, l1, l2):
3+
dummy = ListNode(0)
4+
current, carry = dummy, 0
5+
while l1 != None or l2 != None:
6+
res = carry
7+
if l1 != None:
8+
res += l1.val
9+
l1 = l1.next
10+
if l2 != None:
11+
res += l2.val
12+
l2 = l2.next
13+
carry, res = res / 10, res % 10
14+
current.next = ListNode(res)
15+
current = current.next
16+
if carry == 1:
17+
current.next = ListNode(1)
18+
return dummy.next

Anagrams.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def anagrams(self, strs):
3+
map, result = {}, []
4+
if len(strs) >= 1:
5+
for str in strs:
6+
sorted_str = "".join(sorted(str))
7+
if sorted_str not in map:
8+
map[sorted_str] = [str]
9+
else:
10+
map[sorted_str].append(str)
11+
for word in map:
12+
if len(map[word]) > 1:
13+
result += map[word]
14+
return result

Balanced Binary Tree.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isBalanced(self, root):
3+
return self.getHeight(root) != -1
4+
5+
def getHeight(self, root):
6+
if root == None:
7+
return 0
8+
left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
9+
if left_height < 0 or right_height < 0 or math.fabs(left_height - right_height) > 1:
10+
return -1
11+
return max(left_height, right_height) + 1

Best Time to Buy and Sell Stock II.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxProfit(self, prices):
3+
profit = 0
4+
for i in range(len(prices) - 1):
5+
if (prices[i] < prices[i+1]):
6+
profit += prices[i+1] - prices[i]
7+
return profit
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxProfit(self, prices):
3+
min_price, max_profit = 9223372036854775807, 0
4+
profits_before = [0 for i in range(len(prices))]
5+
for i in range(len(prices)):
6+
min_price = min(prices[i], min_price)
7+
max_profit = max(prices[i] - min_price, max_profit)
8+
profits_before[i] = max_profit
9+
max_price, max_profit = -9223372036854775808, 0
10+
profits_after = [0 for i in range(len(prices))]
11+
for i in reversed(range(len(prices))):
12+
max_price = max(prices[i], max_price)
13+
max_profit = max(max_price - prices[i], max_profit)
14+
profits_after[i] = max_profit
15+
return reduce(lambda acc, i: max(profits_before[i] + profits_after[i], acc), range(len(prices)), 0)

Best Time to Buy and Sell Stock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maxProfit(self, prices):
3+
min_price = 9223372036854775807
4+
max_profit = 0
5+
for i in range(len(prices)):
6+
min_price = min(prices[i], min_price)
7+
max_profit = max(prices[i] - min_price, max_profit)
8+
return max_profit

Binary Tree Inorder Traversal.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def inorderTraversal(self, root):
3+
result = []
4+
self.recur(root, result)
5+
return result
6+
7+
def recur(self, root, result):
8+
if(root == None):
9+
return
10+
self.recur(root.left, result)
11+
result.append(root.val)
12+
self.recur(root.right, result)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def levelOrderBottom(self, root):
3+
result = []
4+
if root == None:
5+
return result
6+
current = [root]
7+
while len(current) > 0:
8+
# Python is just so slow, but who I can complain? I have to write following code to optimize for skewed tree
9+
# -- None of the code from this tag to the next tag is actually needed -- #
10+
if len(current) == 1 and (current[0].left == None or current[0].right== None):
11+
this_node = current[0]
12+
result.insert(0, [this_node.val])
13+
if this_node.left == None and this_node.right != None:
14+
current = [this_node.right]
15+
elif this_node.left != None and this_node.right == None:
16+
current = [this_node.left]
17+
else:
18+
current.pop(0)
19+
else:
20+
# -- None of the code above this tag up to the previous tag is actually needed -- #
21+
vals = []
22+
size = len(current)
23+
for i in range(size):
24+
this_node = current[0]
25+
vals.append(this_node.val)
26+
if i < len(current) - 1:
27+
this_node.next = current[i+1]
28+
if this_node.left != None:
29+
current.append(this_node.left)
30+
if this_node.right != None:
31+
current.append(this_node.right)
32+
current.pop(0)
33+
result.insert(0, vals)
34+
return result

Binary Tree Level Order Traversal.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def levelOrder(self, root):
3+
result = []
4+
if root == None:
5+
return result
6+
current = [root]
7+
while len(current) > 0:
8+
# Python is just so slow, but who I can complain? I have to write following code to optimize for skewed tree
9+
# -- None of the code from this tag to the next tag is actually needed -- #
10+
if len(current) == 1 and (current[0].left == None or current[0].right== None):
11+
this_node = current[0]
12+
result.append([this_node.val])
13+
if this_node.left == None and this_node.right != None:
14+
current = [this_node.right]
15+
elif this_node.left != None and this_node.right == None:
16+
current = [this_node.left]
17+
else:
18+
current.pop(0)
19+
else:
20+
# -- None of the code above this tag up to the previous tag is actually needed -- #
21+
vals = []
22+
size = len(current)
23+
for i in range(size):
24+
this_node = current[0]
25+
vals.append(this_node.val)
26+
if i < len(current) - 1:
27+
this_node.next = current[i+1]
28+
if this_node.left != None:
29+
current.append(this_node.left)
30+
if this_node.right != None:
31+
current.append(this_node.right)
32+
current.pop(0)
33+
result.append(vals)
34+
return result

Binary Tree Postorder Traversal.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def postorderTraversal(self, root):
3+
result = []
4+
self.recur(root, result)
5+
return result
6+
7+
def recur(self, root, result):
8+
if(root == None):
9+
return
10+
self.recur(root.left, result)
11+
self.recur(root.right, result)
12+
result.append(root.val)

Binary Tree Preorder Traversal.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def preorderTraversal(self, root):
3+
result = []
4+
self.recur(root, result)
5+
return result
6+
7+
def recur(self, root, result):
8+
if(root == None):
9+
return
10+
result.append(root.val)
11+
self.recur(root.left, result)
12+
self.recur(root.right, result)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def zigzagLevelOrder(self, root):
3+
result = []
4+
if root == None:
5+
return result
6+
current, reverse = [root], 0
7+
while len(current) > 0:
8+
# Python is just so slow, but who I can complain? I have to write following code to optimize for skewed tree
9+
# -- None of the code from this tag to the next tag is actually needed -- #
10+
if len(current) == 1 and (current[0].left == None or current[0].right== None):
11+
this_node = current[0]
12+
result.append([this_node.val])
13+
if this_node.left == None and this_node.right != None:
14+
current = [this_node.right]
15+
elif this_node.left != None and this_node.right == None:
16+
current = [this_node.left]
17+
else:
18+
current.pop(0)
19+
else:
20+
# -- None of the code above this tag up to the previous tag is actually needed -- #
21+
vals = []
22+
size = len(current)
23+
for i in range(size):
24+
this_node = current[0]
25+
if reverse:
26+
vals.insert(0, this_node.val)
27+
else:
28+
vals.append(this_node.val)
29+
if i < len(current) - 1:
30+
this_node.next = current[i+1]
31+
if this_node.left != None:
32+
current.append(this_node.left)
33+
if this_node.right != None:
34+
current.append(this_node.right)
35+
current.pop(0)
36+
result.append(vals)
37+
reverse = 1 - reverse
38+
return result

Candy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def candy(self, ratings):
3+
candies = [1 for i in range(len(ratings))]
4+
for i in range(1, len(ratings)):
5+
if ratings[i] > ratings[i - 1]:
6+
candies[i] = candies[i - 1] + 1
7+
for i in reversed(range(1, len(ratings))):
8+
if ratings[i - 1] > ratings[i] and candies[i - 1] <= candies[i]:
9+
candies[i - 1] = candies[i] + 1
10+
return reduce(operator.add, candies)

Climbing Stairs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def climbStairs(self, n):
3+
prev, current = 1, 0
4+
for i in range(n + 1):
5+
prev, current = current, prev + current
6+
return current

Combination Sum II.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def combinationSum2(self, candidates, target):
3+
result = []
4+
self.combinationSumRecur(sorted(candidates), result, [], 0, target)
5+
return result
6+
7+
def combinationSumRecur(self, candidates, result, current, start, target):
8+
if target == 0 and current not in result:
9+
result.append(current)
10+
else:
11+
while start < len(candidates) and candidates[start] <= target:
12+
self.combinationSumRecur(candidates, result, current + [candidates[start]], start + 1, target - candidates[start])
13+
start += 1

Combination Sum.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def combinationSum(self, candidates, target):
3+
result = []
4+
self.combinationSumRecur(sorted(candidates), result, [], 0, target)
5+
return result
6+
7+
def combinationSumRecur(self, candidates, result, current, start, target):
8+
if target == 0:
9+
result.append(current)
10+
else:
11+
while start < len(candidates) and candidates[start] <= target:
12+
self.combinationSumRecur(candidates, result, current + [candidates[start]], start, target - candidates[start])
13+
start += 1

Combinations.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def combine(self, n, k):
3+
return self.combineRecur([], n, k, 1)
4+
5+
def combineRecur(self, current, n, k, i):
6+
if k == 0:
7+
return [current]
8+
if i > n:
9+
return []
10+
return self.combineRecur(current, n, k, i + 1) + self.combineRecur(current + [i], n, k - 1, i + 1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def buildTree(self, inorder, postorder):
3+
# using dictionary for index lookup improves the performance of algorithm from O(N^2) to O(N), where N = |postorder|
4+
lookup = {}
5+
for i in range(len(inorder)):
6+
lookup[inorder[i]] = i
7+
return self.buildTreeRecur(lookup, inorder, postorder, 0, len(postorder) - 1, len(postorder) - 1)
8+
9+
def buildTreeRecur(self, lookup, inorder, postorder, in_start, in_end, post_end):
10+
if in_start > in_end:
11+
return None
12+
current = TreeNode(postorder[post_end])
13+
i = lookup[postorder[post_end]]
14+
current.left = self.buildTreeRecur(lookup, inorder, postorder, in_start, i - 1, post_end - (in_end - i) - 1)
15+
current.right = self.buildTreeRecur(lookup, inorder, postorder, i + 1, in_end, post_end - 1)
16+
return current
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def buildTree(self, preorder, inorder):
3+
# using dictionary for index lookup improves the performance of algorithm from O(N^2) to O(N), where N = |preorder|
4+
lookup = {}
5+
for i in range(len(inorder)):
6+
lookup[inorder[i]] = i
7+
return self.buildTreeRecur(lookup, preorder, inorder, 0, len(preorder) - 1, 0)
8+
9+
def buildTreeRecur(self, lookup, preorder, inorder, in_start, in_end, pre_start):
10+
if in_start > in_end:
11+
return None
12+
current = TreeNode(preorder[pre_start])
13+
i = lookup[preorder[pre_start]]
14+
current.left = self.buildTreeRecur(lookup, preorder, inorder, in_start, i - 1, pre_start + 1)
15+
current.right = self.buildTreeRecur(lookup, preorder, inorder, i + 1, in_end, pre_start + i - in_start + 1)
16+
return current

0 commit comments

Comments
 (0)