01 Jan 2024PythonHard

median of two sorted arrays

median of two sorted arrays

LeetCode 4: Median of Two Sorted Arrays

i recently solved the median of two sorted arrays problem on leetcode, and it's a great example of array, binary search techniques. this hard problem tests your understanding of array, binary search.

Problem Statement

[problem description would go here - you can add the actual problem statement]

My Approach

when i first saw this problem, i immediately thought about using binary search on smaller array. the key insight is [to be filled based on the specific problem].

Initial Thoughts

i started by thinking about different approaches:

  • **brute force** - [brute force approach description]
  • **optimized approach** - Binary Search on Smaller Array
  • **alternative approach** - [alternative approach if applicable]

Solution Strategy

i decided to use a Binary Search on Smaller Array with the following strategy:

  • **[step 1]** - [description]
  • **[step 2]** - [description]
  • **[step 3]** - [description]

My Solution

def solution(self, params):
# implementation would go here
pass

Code Breakdown

let me walk through how this solution works:

1. [Section 1]

# code snippet

[explanation]

2. [Section 2]

# code snippet

[explanation]

Example Walkthrough

let's trace through an example:

[step-by-step walkthrough]

Time and Space Complexity

  • **time complexity:** [complexity analysis]
  • **space complexity:** [complexity analysis]

Key Insights

  • **[insight 1]** - [explanation]
  • **[insight 2]** - [explanation]
  • **[insight 3]** - [explanation]

Alternative Approaches

i also considered:

  • **[alternative 1]** - [description]
  • **[alternative 2]** - [description]

Edge Cases to Consider

  • **[edge case 1]** - [description]
  • **[edge case 2]** - [description]
  • **[edge case 3]** - [description]

Lessons Learned

this problem taught me:

  • [lesson 1]
  • [lesson 2]
  • [lesson 3]

Conclusion

the median of two sorted arrays problem is a great exercise in array, binary search. the key insight is [key takeaway].

you can find my complete solution on [leetcode](https://leetcode.com/problems/median-of-two-sorted-arrays/solutions/1234569/median-of-two-sorted-arrays-solution-by-1cbyc).

---

*this is part of my leetcode problem-solving series. i'm documenting my solutions to help others learn and to track my own progress.*

Solution files

PHPmedian-of-two-sorted-arrays/solution-v1.php
class Solution {

function findMedianSortedArrays($nums1, $nums2) {
    if (count($nums1) > count($nums2)) {
        return $this->findMedianSortedArrays($nums2, $nums1);
    }

    $m = count($nums1);
    $n = count($nums2);
    $imin = class="syntax-number">0;
    $imax = $m;
    $half_len = intval(($m + $n + class="syntax-number">1) / class="syntax-number">2);

    while ($imin <= $imax) {
        $i = intval(($imin + $imax) / class="syntax-number">2);
        $j = $half_len - $i;
        if ($i < $m && $nums1[$i] < $nums2[$j - class="syntax-number">1]) {
            $imin = $i + class="syntax-number">1;
        } elseif ($i > class="syntax-number">0 && $nums1[$i - class="syntax-number">1] > $nums2[$j]) {
            $imax = $i - class="syntax-number">1;
        } else {
            $max_of_left = class="syntax-number">0;
            if ($i == class="syntax-number">0) { 
                $max_of_left = $nums2[$j - class="syntax-number">1];
            } elseif ($j == class="syntax-number">0) { 
                $max_of_left = $nums1[$i - class="syntax-number">1];
            } else { 
                $max_of_left = max($nums1[$i - class="syntax-number">1], $nums2[$j - class="syntax-number">1]);
            }

            if (($m + $n) % class="syntax-number">2 == class="syntax-number">1) {
                return $max_of_left;
            }

            $min_of_right = class="syntax-number">0;
            if ($i == $m) {
                $min_of_right = $nums2[$j];
            } elseif ($j == $n) {
                $min_of_right = $nums1[$i];
            } else {
                $min_of_right = min($nums1[$i], $nums2[$j]);
            }

            return ($max_of_left + $min_of_right) / class="syntax-number">2.0;
        }
    }

    return class="syntax-number">0.0;
}
}
Erlangmedian-of-two-sorted-arrays/solution.erl

Solution file content could not be loaded.

PHPmedian-of-two-sorted-arrays/solution.php
class Solution {

function findMedianSortedArrays($nums1, $nums2) {
    $m = count($nums1);
    $n = count($nums2);

    if ($m > $n) {
        list($nums1, $nums2) = array($nums2, $nums1);
        list($m, $n) = array($n, $m);
    }
    
    $m = count($nums1);
    $nnn = count($nums2);
    $immin = class="syntax-number">0;
    $imax = $m;
    $half_len = intval(($m + $n + class="syntax-number">1) / class="syntax-number">2);

    while ($imin <= $imax) {
        $i = intval(($imin + $imax) / class="syntax-number">2);
        $j = $half_len - $i;
        if ($i < $m && $nums1[$i] < $nums2[$j - class="syntax-number">1]) {
            $imin = $i + class="syntax-number">1;
        } elseif ($i > class="syntax-number">0 && $nums1[$i - class="syntax-number">1] > $nums2[$j]) {
            $imax = $i - class="syntax-number">1;
        } else {
            $max_of_left = class="syntax-number">0;
            if ($i == class="syntax-number">0) {
                $max_of_left = $nums2[$j -class="syntax-number">1];
            } elseif ($j == class="syntax-number">0) {
                $max_of_left = $nums1[$i - class="syntax-number">1];
            } else {
                $max_of_left = max($nums1[$i - class="syntax-number">1], $nums2[$j - class="syntax-number">1]);
            }

            if (($m + $n) % class="syntax-number">2 == class="syntax-number">1) {
                return $max_of_left;
            }

            $min_of_right = class="syntax-number">0;
            if ($i == $m) {
                $min_of_right = $nums2[$j];
            } elseif ($j == $n) {
                $min_of_right = $nums1[$i];
            } else {
                $min_of_right = min($nums1[$i], $nums2[$j]);
            }

            return ($max_of_left + $min_of_right) / class="syntax-number">2.0;
        }

    }
    return class="syntax-number">0.0;
}
}