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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import List

class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
"""
Find the median of two sorted arrays using binary search.
Time complexity: O(log(min(m, n)))
Space complexity: O(1)
"""
# Ensure nums1 is the smaller array for efficiency
if len(nums1) > len(nums2):
return self.findMedianSortedArrays(nums2, nums1)

m, n = len(nums1), len(nums2)
left, right = 0, m

while left <= right:
# Partition nums1
partition1 = (left + right) // 2
# Partition nums2 such that left side has half the total elements
partition2 = (m + n + 1) // 2 - partition1

# Handle edge cases where partition is at the boundary
max_left1 = float('-inf') if partition1 == 0 else nums1[partition1 - 1]
min_right1 = float('inf') if partition1 == m else nums1[partition1]

max_left2 = float('-inf') if partition2 == 0 else nums2[partition2 - 1]
min_right2 = float('inf') if partition2 == n else nums2[partition2]

# Check if we found the correct partition
if max_left1 <= min_right2 and max_left2 <= min_right1:
# If total length is even
if (m + n) % 2 == 0:
return (max(max_left1, max_left2) + min(min_right1, min_right2)) / 2
else:
# If total length is odd
return max(max_left1, max_left2)
elif max_left1 > min_right2:
# Move partition1 to the left
right = partition1 - 1
else:
# Move partition1 to the right
left = partition1 + 1

raise ValueError("Input arrays are not sorted")
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function findMedianSortedArrays(nums1: number[], nums2: number[]): number {
// Ensure nums1 is the smaller array for efficiency
if (nums1.length > nums2.length) {
return findMedianSortedArrays(nums2, nums1);
}

const m = nums1.length;
const n = nums2.length;
let left = 0;
let right = m;

while (left <= right) {
// Partition nums1
const partition1 = Math.floor((left + right) / 2);
// Partition nums2 such that left side has half the total elements
const partition2 = Math.floor((m + n + 1) / 2) - partition1;

// Handle edge cases where partition is at the boundary
const maxLeft1 = partition1 === 0 ? -Infinity : nums1[partition1 - 1];
const minRight1 = partition1 === m ? Infinity : nums1[partition1];

const maxLeft2 = partition2 === 0 ? -Infinity : nums2[partition2 - 1];
const minRight2 = partition2 === n ? Infinity : nums2[partition2];

// Check if we found the correct partition
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
// If total length is even
if ((m + n) % 2 === 0) {
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2;
} else {
// If total length is odd
return Math.max(maxLeft1, maxLeft2);
}
} else if (maxLeft1 > minRight2) {
// Move partition1 to the left
right = partition1 - 1;
} else {
// Move partition1 to the right
left = partition1 + 1;
}
}

throw new Error("Input arrays are not sorted");
}
Loading
Loading