Skip to content

Commit d640e2b

Browse files
authored
solved errors
1 parent 4ec56af commit d640e2b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ 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 <code>$O(log (m+n))$</code>.
19+
The overall run time complexity should be $O(log (m+n))$.
2020

2121
### Example 1
2222

@@ -429,7 +429,7 @@ Let's test the solution with the sample test cases:
429429
Input: nums1 = [1, 3], nums2 = [2]
430430
Output: 2.00000
431431
Explanation: merged array = [1, 2, 3] and median is 2.
432-
````
432+
```
433433
</TabItem>
434434

435435
<TabItem value="TestCase2" label="Case 2">
@@ -770,8 +770,7 @@ Let's test the solution with the sample test cases:
770770
Input: nums1 = [1, 3], nums2 = [2]
771771
Output: 2.00000
772772
Explanation: merged array = [1, 2, 3] and median is 2.
773-
````
774-
773+
```
775774
</TabItem>
776775
<TabItem value="TestCase2" label="Case 2">
777776
```plaintext
@@ -786,7 +785,7 @@ Explanation: merged array = [1, 2, 3, 4] and median is (2 + 3) / 2 = 2.5.
786785

787786
:::info
788787

789-
**Note**: The binary search approach is more efficient than the divide and conquer approach as it has a better time complexity of <code>$O(log(min(n, m)))$</code> compared to the divide and conquer approach. However, both approaches provide a solution to the problem of finding the median of two sorted arrays.
788+
**Note**: The binary search approach is more efficient than the divide and conquer approach as it has a better time complexity of $O(log(min(n, m)))$ compared to the divide and conquer approach. However, both approaches provide a solution to the problem of finding the median of two sorted arrays.
790789

791790
**Which approach is best for you?**
792791

@@ -795,7 +794,7 @@ The binary search approach is more efficient and recommended for solving the pro
795794
:::
796795

797796
:::tip
798-
When asked to find the median of two sorted arrays, a direct approach that merges the two arrays and then finds the median will work but isn't optimal. Given the problem's constraints, we can leverage the fact that the arrays are already sorted and use binary search to find the median in $$ O(\log(\min(n, m))) $$ time complexity.
797+
When asked to find the median of two sorted arrays, a direct approach that merges the two arrays and then finds the median will work but isn't optimal. Given the problem's constraints, we can leverage the fact that the arrays are already sorted and use binary search to find the median in $O(\log(\min(n, m)))$ time complexity.
799798

800799
The key idea is to use binary search to partition the smaller array in such a way that we can easily find the median by comparing elements around the partition.
801800

@@ -823,19 +822,19 @@ The key idea is to use binary search to partition the smaller array in such a wa
823822

824823
4. **Boundary Conditions**:
825824

826-
- 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} $$.
825+
- 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} $$.
827826

828827
5. **Check Valid Partition**:
829828

830829
- 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:
831830
$$
832831
\text{maxX} \leq \text{minY} \quad \text{and} \quad \text{maxY} \leq \text{minX}
833832
$$
834-
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} $$.
833+
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}$.
835834

836835
6. **Calculate the Median**:
837836

838-
- If the total number of elements $$ (x + y) $$ is even, the median is the average of the two middle values:
837+
- If the total number of elements $(x + y)$ is even, the median is the average of the two middle values:
839838
$$
840839
\text{median} = \frac{\text{max(maxX, maxY)} + \text{min(minX, minY)}}{2}
841840
$$
@@ -845,8 +844,8 @@ The key idea is to use binary search to partition the smaller array in such a wa
845844
$$
846845

847846
7. **Adjust Binary Search**:
848-
- If $$ \text{maxX} > \text{minY} $$, it means we need to move the partition in $$ \text{nums1} $$ to the left, so adjust $$ \text{high} $$.
849-
- If $$ \text{maxY} > \text{minX} $$, it means we need to move the partition in $$ \text{nums1} $$ to the right, so adjust $$ \text{low} $$.
847+
- If $\text{maxX} > \text{minY}$, it means we need to move the partition in $\text{nums1}$ to the left, so adjust $\text{high}$.
848+
- If $\text{maxY} > \text{minX}$, it means we need to move the partition in $\text{nums1}$ to the right, so adjust $\text{low}$.
850849

851850
:::
852851

0 commit comments

Comments
 (0)