| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MedianOfTwoSortedArrays; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MedianOfTwoSortedArraysBinarySearch : IMedianOfTwoSortedArrays |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(log min(n,m)) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums1"></param> |
| | 22 | | /// <param name="nums2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public double FindMedianSortedArrays(int[] nums1, int[] nums2) |
| 16 | 25 | | { |
| 22 | 26 | | while (true) |
| 22 | 27 | | { |
| 22 | 28 | | if (nums1.Length > nums2.Length) |
| 6 | 29 | | { |
| 6 | 30 | | (nums1, nums2) = (nums2, nums1); |
| | 31 | |
|
| 6 | 32 | | continue; |
| | 33 | | } |
| | 34 | |
|
| 16 | 35 | | var low = 0; |
| 16 | 36 | | var high = nums1.Length; |
| 16 | 37 | | var halfLen = (nums1.Length + nums2.Length + 1) / 2; |
| | 38 | |
|
| 26 | 39 | | while (low <= high) |
| 26 | 40 | | { |
| 26 | 41 | | var i = (low + high) / 2; |
| 26 | 42 | | var j = halfLen - i; |
| | 43 | |
|
| 26 | 44 | | if (i < nums1.Length && nums2[j - 1] > nums1[i]) |
| 9 | 45 | | { |
| 9 | 46 | | low = i + 1; |
| 9 | 47 | | } |
| 17 | 48 | | else if (i > 0 && nums1[i - 1] > nums2[j]) |
| 1 | 49 | | { |
| 1 | 50 | | high = i - 1; |
| 1 | 51 | | } |
| | 52 | | else |
| 16 | 53 | | { |
| | 54 | | int maxLeft; |
| | 55 | |
|
| 16 | 56 | | if (i == 0) |
| 3 | 57 | | { |
| 3 | 58 | | maxLeft = nums2[j - 1]; |
| 3 | 59 | | } |
| 13 | 60 | | else if (j == 0) |
| 1 | 61 | | { |
| 1 | 62 | | maxLeft = nums1[i - 1]; |
| 1 | 63 | | } |
| | 64 | | else |
| 12 | 65 | | { |
| 12 | 66 | | maxLeft = Math.Max(nums1[i - 1], nums2[j - 1]); |
| 12 | 67 | | } |
| | 68 | |
|
| 16 | 69 | | if ((nums1.Length + nums2.Length) % 2 == 1) |
| 9 | 70 | | { |
| 9 | 71 | | return maxLeft; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | int minRight; |
| | 75 | |
|
| 7 | 76 | | if (i == nums1.Length) |
| 3 | 77 | | { |
| 3 | 78 | | minRight = nums2[j]; |
| 3 | 79 | | } |
| 4 | 80 | | else if (j == nums2.Length) |
| 0 | 81 | | { |
| 0 | 82 | | minRight = nums1[i]; |
| 0 | 83 | | } |
| | 84 | | else |
| 4 | 85 | | { |
| 4 | 86 | | minRight = Math.Min(nums1[i], nums2[j]); |
| 4 | 87 | | } |
| | 88 | |
|
| 7 | 89 | | return (maxLeft + minRight) / 2.0; |
| | 90 | | } |
| 10 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | return 0; |
| | 94 | | } |
| 16 | 95 | | } |
| | 96 | | } |