| | 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.MergeSortedArray; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MergeSortedArrayMergingAndSorting : IMergeSortedArray |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O((m+n) log(m+n)) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums1"></param> |
| | 22 | | /// <param name="m"></param> |
| | 23 | | /// <param name="nums2"></param> |
| | 24 | | /// <param name="n"></param> |
| | 25 | | public void Merge(int[] nums1, int m, int[] nums2, int n) |
| 4 | 26 | | { |
| 4 | 27 | | if (m == 0) |
| 2 | 28 | | { |
| 16 | 29 | | for (var i = 0; i < n; i++) |
| 6 | 30 | | { |
| 6 | 31 | | nums1[i] = nums2[i]; |
| 6 | 32 | | } |
| 2 | 33 | | } |
| | 34 | | else |
| 2 | 35 | | { |
| 15 | 36 | | for (int i = m, j = 0; i < m + n; i++, j++) |
| 3 | 37 | | { |
| 3 | 38 | | nums1[i] = nums2[j]; |
| 3 | 39 | | } |
| | 40 | |
|
| 2 | 41 | | Array.Sort(nums1); |
| 2 | 42 | | } |
| 4 | 43 | | } |
| | 44 | | } |