| | 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 MedianOfTwoSortedArraysIterative : IMedianOfTwoSortedArrays |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m) |
| | 19 | | /// Space complexity - O(n + m) |
| | 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 | | { |
| 16 | 26 | | var concat = new int[nums1.Length + nums2.Length]; |
| | 27 | |
|
| 16 | 28 | | var nums1Index = 0; |
| 16 | 29 | | var nums2Index = 0; |
| | 30 | |
|
| 103 | 31 | | while (nums1Index < nums1.Length || nums2Index < nums2.Length) |
| 87 | 32 | | { |
| 87 | 33 | | int? num1 = null; |
| | 34 | |
|
| 87 | 35 | | if (nums1Index < nums1.Length) |
| 60 | 36 | | { |
| 60 | 37 | | num1 = nums1[nums1Index]; |
| 60 | 38 | | } |
| | 39 | |
|
| 87 | 40 | | int? num2 = null; |
| | 41 | |
|
| 87 | 42 | | if (nums2Index < nums2.Length) |
| 80 | 43 | | { |
| 80 | 44 | | num2 = nums2[nums2Index]; |
| 80 | 45 | | } |
| | 46 | |
|
| 87 | 47 | | if (num1.HasValue && num2.HasValue) |
| 53 | 48 | | { |
| 53 | 49 | | if (num1 < num2) |
| 33 | 50 | | { |
| 33 | 51 | | concat[nums1Index + nums2Index] = num1.Value; |
| | 52 | |
|
| 33 | 53 | | nums1Index++; |
| 33 | 54 | | } |
| | 55 | | else |
| 20 | 56 | | { |
| 20 | 57 | | concat[nums1Index + nums2Index] = num2.Value; |
| | 58 | |
|
| 20 | 59 | | nums2Index++; |
| 20 | 60 | | } |
| 53 | 61 | | } |
| 34 | 62 | | else if (num1.HasValue) |
| 7 | 63 | | { |
| 7 | 64 | | concat[nums1Index + nums2Index] = num1.Value; |
| | 65 | |
|
| 7 | 66 | | nums1Index++; |
| 7 | 67 | | } |
| 27 | 68 | | else if (num2.HasValue) |
| 27 | 69 | | { |
| 27 | 70 | | concat[nums1Index + nums2Index] = num2.Value; |
| | 71 | |
|
| 27 | 72 | | nums2Index++; |
| 27 | 73 | | } |
| 87 | 74 | | } |
| | 75 | |
|
| 16 | 76 | | var index = concat.Length / 2; |
| | 77 | |
|
| 16 | 78 | | if (concat.Length % 2 == 0) |
| 7 | 79 | | { |
| 7 | 80 | | return (concat[index - 1] + concat[index]) / 2.0; |
| | 81 | | } |
| | 82 | |
|
| 9 | 83 | | return concat[index]; |
| 16 | 84 | | } |
| | 85 | | } |