| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.MinimumEqualSumOfTwoArraysAfterReplacingZeros; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumEqualSumOfTwoArraysAfterReplacingZerosGreedy : IMinimumEqualSumOfTwoArraysAfterReplacingZeros |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n + m) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums1"></param> |
| | | 22 | | /// <param name="nums2"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public long MinSum(int[] nums1, int[] nums2) |
| | 6 | 25 | | { |
| | 6 | 26 | | long nums1Sum = 0; |
| | 6 | 27 | | var nums1ZeroCount = 0; |
| | | 28 | | |
| | 116 | 29 | | foreach (var num in nums1) |
| | 49 | 30 | | { |
| | 49 | 31 | | if (num == 0) |
| | 18 | 32 | | { |
| | 18 | 33 | | nums1Sum++; |
| | 18 | 34 | | nums1ZeroCount++; |
| | 18 | 35 | | } |
| | | 36 | | else |
| | 31 | 37 | | { |
| | 31 | 38 | | nums1Sum += num; |
| | 31 | 39 | | } |
| | 49 | 40 | | } |
| | | 41 | | |
| | 6 | 42 | | long nums2Sum = 0; |
| | 6 | 43 | | var nums2ZeroCount = 0; |
| | | 44 | | |
| | 110 | 45 | | foreach (var num in nums2) |
| | 46 | 46 | | { |
| | 46 | 47 | | if (num == 0) |
| | 5 | 48 | | { |
| | 5 | 49 | | nums2ZeroCount++; |
| | 5 | 50 | | nums2Sum++; |
| | 5 | 51 | | } |
| | | 52 | | else |
| | 41 | 53 | | { |
| | 41 | 54 | | nums2Sum += num; |
| | 41 | 55 | | } |
| | 46 | 56 | | } |
| | | 57 | | |
| | 6 | 58 | | if ((nums2Sum > nums1Sum && nums1ZeroCount == 0) || |
| | 6 | 59 | | (nums1Sum > nums2Sum && nums2ZeroCount == 0)) |
| | 1 | 60 | | { |
| | 1 | 61 | | return -1; |
| | | 62 | | } |
| | | 63 | | |
| | 5 | 64 | | return Math.Max(nums1Sum, nums2Sum); |
| | 6 | 65 | | } |
| | | 66 | | } |