Skip to content

Commit 21abd1d

Browse files
committed
Initial commit
0 parents  commit 21abd1d

13 files changed

+332
-0
lines changed

Diff for: 110_balanced-binary-tree.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://leetcode.com/problems/balanced-binary-tree/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
10+
from collections import deque
11+
class Solution:
12+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
13+
is_balanced = True
14+
15+
def dfs(root):
16+
nonlocal is_balanced
17+
18+
if root is None:
19+
return 0
20+
21+
left = dfs(root.left)
22+
right = dfs(root.right)
23+
24+
if abs(left - right) > 1:
25+
is_balanced = False
26+
27+
return max(left, right) + 1
28+
29+
30+
dfs(root)
31+
return is_balanced

Diff for: 121_best-time-to-buy-and-sell-stock.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
import sys
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
min_price = sys.maxsize
6+
res = 0
7+
8+
for p in prices:
9+
min_price = min(p, min_price)
10+
res = max(res, p - min_price)
11+
12+
return res

Diff for: 125_valid-palindrome.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# https://leetcode.com/problems/valid-palindrome/
2+
import re
3+
class Solution:
4+
def isPalindrome(self, s: str) -> bool:
5+
s = s.lower()
6+
s = "".join(re.findall("[A-Za-z0-9]", s))
7+
i, j = 0, len(s) - 1
8+
9+
while i < j:
10+
if s[i] != s[j]:
11+
return False
12+
13+
i += 1
14+
j -= 1
15+
16+
return True

Diff for: 141_linked-list-cycle.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# https://leetcode.com/problems/linked-list-cycle/
2+
3+
# Definition for singly-linked list.
4+
# class ListNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.next = None
8+
9+
class Solution:
10+
def hasCycle(self, head: Optional[ListNode]) -> bool:
11+
if head is None:
12+
return False
13+
14+
#O(n^2) time, O(1) space
15+
cur_node = head
16+
cur_node_delayed = head
17+
18+
if cur_node.next is None:
19+
return False
20+
21+
if cur_node.next.next is None:
22+
return False
23+
24+
25+
while cur_node.next is not None and cur_node.next.next is not None:
26+
cur_node = cur_node.next.next
27+
cur_node_delayed = cur_node_delayed.next
28+
29+
if cur_node == cur_node_delayed:
30+
return True
31+
32+
return False
33+
34+
35+
def hasCycle(self, head: Optional[ListNode]) -> bool:
36+
# O(n) time, O(n) space
37+
n_addresses = set()
38+
39+
cur_node = head
40+
while cur_node.next is not None:
41+
if id(cur_node) in n_addresses:
42+
return True
43+
n_addresses.add(id(cur_node))
44+
cur_node = cur_node.next
45+
46+
return False

Diff for: 1_two_sum.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://leetcode.com/problems/two-sum/
2+
3+
class Solution:
4+
def twoSumBruteForce(self, nums: List[int], target: int) -> List[int]:
5+
"""O(n^2) with O(1) space"""
6+
for i in range(0, len(nums)):
7+
for j in range(i+1, len(nums)):
8+
if nums[i] + nums[j] == target:
9+
return i, j
10+
11+
def twoSumFaster(self, nums: List[int], target: int) -> List[int]:
12+
"""O(nlogn) with O(n) space"""
13+
14+
i, j = 0, len(nums) - 1
15+
16+
nums2 = [(x, i) for i, x in enumerate(nums)]
17+
nums2.sort(key=lambda x: x[0])
18+
19+
ij_sum = nums2[i][0] + nums2[j][0]
20+
while ij_sum != target:
21+
22+
if ij_sum > target:
23+
j-=1
24+
else:
25+
i+=1
26+
27+
ij_sum = nums2[i][0] + nums2[j][0]
28+
29+
return nums2[i][1], nums2[j][1]
30+
31+
32+
def twoSum(self, nums: List[int], target: int) -> List[int]:
33+
"""
34+
O(n) w/ O(n) space - most optimal and simplest logic
35+
"""
36+
# Visited values as dict keys with their indexes as dict values
37+
visited_values = {}
38+
for i, x in enumerate(nums):
39+
if (target - x) in visited_values.keys():
40+
return i, visited_values[target - x]
41+
else:
42+
visited_values[x] = i

Diff for: 20_valid_parantheses.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://leetcode.com/problems/valid-parentheses/
2+
3+
from collections import deque
4+
class Solution:
5+
def isValid(self, s: str) -> bool:
6+
stack = deque()
7+
for c in s:
8+
if c in "([{":
9+
stack.append(c)
10+
11+
if c in ")]}":
12+
if len(stack) == 0:
13+
return False
14+
15+
opening_bracket = stack.pop()
16+
17+
if opening_bracket == "(":
18+
expected_closing_bracket = ")"
19+
elif opening_bracket == "[":
20+
expected_closing_bracket = "]"
21+
else:
22+
expected_closing_bracket = "}"
23+
24+
if c != expected_closing_bracket:
25+
return False
26+
27+
return True if len(stack) == 0 else False

Diff for: 21_merge_two_sorted_lists.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# https://leetcode.com/problems/merge-two-sorted-lists/
2+
3+
# Definition for singly-linked list.
4+
# class ListNode:
5+
# def __init__(self, val=0, next=None):
6+
# self.val = val
7+
# self.next = next
8+
class Solution:
9+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
10+
if list1 is None or list2 is None:
11+
if list1 is not None:
12+
return list1
13+
14+
if list2 is not None:
15+
return list2
16+
17+
return None
18+
19+
l1_p = list1
20+
l2_p = list2
21+
res_head = list2 if l1_p.val >= l2_p.val else list1
22+
23+
while l1_p and l2_p:
24+
if l1_p.val >= l2_p.val:
25+
26+
while l2_p.next is not None and l1_p.val >= l2_p.next.val:
27+
l2_p = l2_p.next
28+
29+
l2_p.next, l2_p = l1_p, l2_p.next
30+
31+
else:
32+
33+
while l1_p.next is not None and l2_p.val >= l1_p.next.val:
34+
l1_p = l1_p.next
35+
36+
l1_p.next, l1_p = l2_p, l1_p.next
37+
38+
39+
return res_head

Diff for: 226_invert_binary_tree.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# https://leetcode.com/problems/invert-binary-tree/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
11+
if root is None:
12+
return None
13+
14+
root.left, root.right = root.right, root.left
15+
16+
self.invertTree(root.left)
17+
self.invertTree(root.right)
18+
19+
return root
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.left = None
8+
# self.right = None
9+
10+
class Solution:
11+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
12+
if p.val > root.val and q.val > root.val: return self.lowestCommonAncestor(root.right, p, q)
13+
elif p.val < root.val and q.val < root.val: return self.lowestCommonAncestor(root.left, p, q)
14+
else: return root

Diff for: 242_valid_anagram.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://leetcode.com/problems/valid-anagram/
2+
class Solution:
3+
def isAnagram(self, s: str, t: str) -> bool:
4+
chars = [0]*26
5+
6+
for c in s:
7+
chars[ord(c) - ord("a")] += 1
8+
9+
for c in t:
10+
chars[ord(c) - ord("a")] -= 1
11+
12+
return all(c == 0 for c in chars)

Diff for: 53_maximum_subarray.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# https://leetcode.com/problems/maximum-subarray/
2+
3+
class Solution:
4+
def maxSubArrayNTime(self, nums: List[int]) -> int:
5+
cur = -10001
6+
max_res = -10001
7+
for num in nums:
8+
cur = max(num, num + cur)
9+
max_res = max(cur, max_res)
10+
return max_res
11+
12+
13+
def maxSubArray(self, nums: List[int]) -> int:
14+
if len(nums) == 1:
15+
return nums
16+
17+
l = self.maxSubArray(nums[:len(nums)//2])
18+
r = self.maxSubArray(nums[len(nums)//2:])
19+
20+
return max(r, l+r)
21+

Diff for: 704_binary_search.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# https://leetcode.com/problems/binary-search/
2+
3+
func search(nums []int, target int) int {
4+
5+
if len(nums) == 0 {
6+
return -1
7+
}
8+
9+
start_index := 0
10+
end_index := len(nums) - 1
11+
mid_index := ((end_index - start_index) / 2) + start_index
12+
13+
for start_index != end_index && start_index < len(nums) && end_index >= 0 {
14+
if target == nums[mid_index] {
15+
return mid_index
16+
}
17+
if target < nums[mid_index]{
18+
end_index = mid_index - 1
19+
} else {
20+
start_index = mid_index + 1
21+
}
22+
mid_index = ((end_index - start_index) / 2) + start_index
23+
}
24+
25+
if start_index < len(nums) && end_index >= 0 && target == nums[mid_index] {
26+
return start_index
27+
}
28+
29+
return -1
30+
31+
32+
}

Diff for: 733_flood_fill.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
3+
if color == image[sr][sc] or len(image) == 0 or len(image[0]) == 0:
4+
return image
5+
6+
image_pixel = image[sr][sc]
7+
image[sr][sc] = color
8+
9+
if sr - 1 >= 0 and image[sr - 1][sc] == image_pixel:
10+
self.floodFill(image, sr - 1, sc, color)
11+
12+
if sr + 1 < len(image) and image[sr + 1][sc] == image_pixel:
13+
self.floodFill(image, sr + 1, sc, color)
14+
15+
if sc - 1 >= 0 and image[sr][sc - 1] == image_pixel:
16+
self.floodFill(image, sr, sc - 1, color)
17+
18+
if sc + 1 < len(image[0]) and image[sr][sc + 1] == image_pixel:
19+
self.floodFill(image, sr, sc + 1, color)
20+
21+
return image

0 commit comments

Comments
 (0)