| | 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.MergeTwo2DArraysBySummingValues; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MergeTwo2DArraysBySummingValuesSortedDictionary : IMergeTwo2DArraysBySummingValues |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n + m log m) |
| | 19 | | /// Space complexity - O((n + m) * log (n + m)) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums1"></param> |
| | 22 | | /// <param name="nums2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int[][] MergeArrays(int[][] nums1, int[][] nums2) |
| 2 | 25 | | { |
| 2 | 26 | | var dictionary = new SortedDictionary<int, int>(); |
| | 27 | |
|
| 18 | 28 | | foreach (var num in nums1) |
| 6 | 29 | | { |
| 6 | 30 | | dictionary[num[0]] = num[1]; |
| 6 | 31 | | } |
| | 32 | |
|
| 16 | 33 | | foreach (var num in nums2) |
| 5 | 34 | | { |
| 5 | 35 | | if (dictionary.ContainsKey(num[0])) |
| 2 | 36 | | { |
| 2 | 37 | | dictionary[num[0]] += num[1]; |
| 2 | 38 | | } |
| | 39 | | else |
| 3 | 40 | | { |
| 3 | 41 | | dictionary[num[0]] = num[1]; |
| 3 | 42 | | } |
| 5 | 43 | | } |
| | 44 | |
|
| 2 | 45 | | var result = new int[dictionary.Count][]; |
| | 46 | |
|
| 2 | 47 | | var i = 0; |
| | 48 | |
|
| 24 | 49 | | foreach (var keyValuePair in dictionary) |
| 9 | 50 | | { |
| 9 | 51 | | result[i] = [keyValuePair.Key, keyValuePair.Value]; |
| | 52 | |
|
| 9 | 53 | | i++; |
| 9 | 54 | | } |
| | 55 | |
|
| 2 | 56 | | return result; |
| 2 | 57 | | } |
| | 58 | | } |