Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Median_of_Two_Sorted_Arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution:
def findMedianSortedArrays(
self, nums1: List[int], nums2: List[int]
) -> float:
m, n = len(nums1), len(nums2)
p1, p2 = 0, 0

# Get the smaller value between nums1[p1] and nums2[p2].
def get_min():
nonlocal p1, p2
if p1 < m and p2 < n:
if nums1[p1] < nums2[p2]:
ans = nums1[p1]
p1 += 1
else:
ans = nums2[p2]
p2 += 1
elif p2 == n:
ans = nums1[p1]
p1 += 1
else:
ans = nums2[p2]
p2 += 1
return ans

if (m + n) % 2 == 0:
for _ in range((m + n) // 2 - 1):
_ = get_min()
return (get_min() + get_min()) / 2
else:
for _ in range((m + n) // 2):
_ = get_min()
return get_min()



## Example usage:# sol = Solution()
# print(sol.findMedianSortedArrays([1, 3], [2])) # Output: 2.0
# print(sol.findMedianSortedArrays([1, 2], [3, 4])) # Output: 2.5

# time complexity: O(m + n) space complexity: O(1)


43 changes: 43 additions & 0 deletions Python/Median_of_Two_Sorted_Arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution:
def findMedianSortedArrays(
self, nums1: List[int], nums2: List[int]
) -> float:
m, n = len(nums1), len(nums2)
p1, p2 = 0, 0

# Get the smaller value between nums1[p1] and nums2[p2].
def get_min():
nonlocal p1, p2
if p1 < m and p2 < n:
if nums1[p1] < nums2[p2]:
ans = nums1[p1]
p1 += 1
else:
ans = nums2[p2]
p2 += 1
elif p2 == n:
ans = nums1[p1]
p1 += 1
else:
ans = nums2[p2]
p2 += 1
return ans

if (m + n) % 2 == 0:
for _ in range((m + n) // 2 - 1):
_ = get_min()
return (get_min() + get_min()) / 2
else:
for _ in range((m + n) // 2):
_ = get_min()
return get_min()



## Example usage:# sol = Solution()
# print(sol.findMedianSortedArrays([1, 3], [2])) # Output: 2.0
# print(sol.findMedianSortedArrays([1, 2], [3, 4])) # Output: 2.5

# time complexity: O(m + n) space complexity: O(1)


61 changes: 61 additions & 0 deletions Python/balancing_bst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:
def balanceBST(self, root: TreeNode) -> TreeNode:
# Step 1: Perform In-Order Traversal to get a sorted list of values
nums = []
def inorder_traversal(node):
if not node:
return
inorder_traversal(node.left)
nums.append(node.val)
inorder_traversal(node.right)

inorder_traversal(root)

# Step 2: Build a Balanced BST from the sorted list
#
def build_balanced_bst(l, r):
# Base Case: If the left index exceeds the right index, the subarray is empty.
if l > r:
return None

# Find the middle element to use as the root
mid = (l + r) // 2

# Create the root node
root_node = TreeNode(nums[mid])

# Recursively build the left subtree from the left half of the array
root_node.left = build_balanced_bst(l, mid - 1)

# Recursively build the right subtree from the right half of the array
root_node.right = build_balanced_bst(mid + 1, r)

return root_node

# Start building the balanced BST using the entire sorted list
return build_balanced_bst(0, len(nums) - 1)

# Example Usage (optional):
# unbalanced_root = TreeNode(1)
# unbalanced_root.right = TreeNode(2)
# unbalanced_root.right.right = TreeNode(3)
# unbalanced_root.right.right.right = TreeNode(4)
#
# balanced_root = Solution().balanceBST(unbalanced_root)
# # The balanced_root structure will be:
# # 3
# # / \
# # 2 4
# # /
# # 1
# time complexity: O(n)
# space complexity: O(n)