Skip to content

Commit f3e6d7a

Browse files
2 parents b764e38 + d640e2b commit f3e6d7a

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

dsa-solutions/lc-solutions/0000-0099/0004-Median-of-two-Sorted-Array.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ In this page, we will solve the problem [Median of Two Sorted Arrays](https://le
1616

1717
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return the median of the two sorted arrays.
1818

19-
The overall run time complexity should be $O(log (m+n))$.
20-
2119
### Example 1
2220

2321
```plaintext
@@ -449,7 +447,7 @@ Let's test the solution with the sample test cases:
449447
Input: nums1 = [1, 3], nums2 = [2]
450448
Output: 2.00000
451449
Explanation: merged array = [1, 2, 3] and median is 2.
452-
````
450+
```
453451
</TabItem>
454452

455453
<TabItem value="TestCase2" label="Case 2">
@@ -738,8 +736,7 @@ Let's test the solution with the sample test cases:
738736
Input: nums1 = [1, 3], nums2 = [2]
739737
Output: 2.00000
740738
Explanation: merged array = [1, 2, 3] and median is 2.
741-
````
742-
739+
```
743740
</TabItem>
744741
<TabItem value="TestCase2" label="Case 2">
745742
```plaintext
@@ -790,19 +787,19 @@ The key idea is to use binary search to partition the smaller array in such a wa
790787

791788
4. **Boundary Conditions**:
792789

793-
- Handle cases where partitions might go out of bounds. If $$ \text{partitionX} $$ is 0, it means there are no elements on the left side of $$ \text{nums1} $$. If $$ \text{partitionX} $$ is $$ x $$, it means there are no elements on the right side of $$ \text{nums1} $$.
790+
- Handle cases where partitions might go out of bounds. If $$ \text{partitionX} $$ is 0, it means there are no elements on the left side of $$ \text{nums1} $$. If $$ \text{partitionX} $$ is $x$, it means there are no elements on the right side of $$ \text{nums1} $$.
794791

795792
5. **Check Valid Partition**:
796793

797794
- A valid partition is one where the maximum element on the left side of both partitions is less than or equal to the minimum element on the right side of both partitions:
798795
$$
799796
\text{maxX} \leq \text{minY} \quad \text{and} \quad \text{maxY} \leq \text{minX}
800797
$$
801-
Here, $$ \text{maxX} $$ is the largest element on the left side of $$ \text{nums1} $$, $$ \text{minX} $$ is the smallest element on the right side of $$ \text{nums1} $$, and similarly for $$ \text{nums2} $$.
798+
Here, $\text{maxX}$ is the largest element on the left side of $\text{nums1}$, $\text{minX}$ is the smallest element on the right side of $\text{nums1}$, and similarly for $\text{nums2}$.
802799

803800
6. **Calculate the Median**:
804801

805-
- If the total number of elements $ (x + y) $ is even, the median is the average of the two middle values:
802+
- If the total number of elements $$ (x + y) $$ is even, the median is the average of the two middle values:
806803
$$
807804
\text{median} = \frac{\text{max(maxX, maxY)} + \text{min(minX, minY)}}{2}
808805
$$
@@ -812,8 +809,8 @@ The key idea is to use binary search to partition the smaller array in such a wa
812809
$$
813810

814811
7. **Adjust Binary Search**:
815-
- If $$ \text{maxX} > \text{minY} $$, it means we need to move the partition in $$ \text{nums1} $$ to the left, so adjust $$ \text{high} $$.
816-
- If $$ \text{maxY} > \text{minX} $$, it means we need to move the partition in $$ \text{nums1} $$ to the right, so adjust $$ \text{low} $$.
812+
- If $\text{maxX} > \text{minY}$, it means we need to move the partition in $\text{nums1}$ to the left, so adjust $\text{high}$.
813+
- If $\text{maxY} > \text{minX}$, it means we need to move the partition in $\text{nums1}$ to the right, so adjust $\text{low}$.
817814

818815
:::
819816

0 commit comments

Comments
 (0)