| | | 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.GreatestSumDivisibleByThree; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class GreatestSumDivisibleByThreeGreedy : IGreatestSumDivisibleByThree |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public int MaxSumDivThree(int[] nums) |
| | 3 | 24 | | { |
| | 3 | 25 | | var sum = 0; |
| | | 26 | | |
| | 3 | 27 | | var smallestNumberWithRemainderOne = int.MaxValue; |
| | 3 | 28 | | var secondSmallestNumberWithRemainderOne = int.MaxValue; |
| | 3 | 29 | | var smallestNumberWithRemainderTwo = int.MaxValue; |
| | 3 | 30 | | var secondSmallestNumberWithRemainderTwo = int.MaxValue; |
| | | 31 | | |
| | 28 | 32 | | for (var i = 0; i < nums.Length; i++) |
| | 11 | 33 | | { |
| | 11 | 34 | | var num = nums[i]; |
| | | 35 | | |
| | 11 | 36 | | sum += num; |
| | | 37 | | |
| | 11 | 38 | | var remainder = num % 3; |
| | | 39 | | |
| | 11 | 40 | | switch (remainder) |
| | | 41 | | { |
| | 5 | 42 | | case 1 when num < smallestNumberWithRemainderOne: |
| | 3 | 43 | | secondSmallestNumberWithRemainderOne = smallestNumberWithRemainderOne; |
| | 3 | 44 | | smallestNumberWithRemainderOne = num; |
| | | 45 | | |
| | 3 | 46 | | break; |
| | | 47 | | case 1: |
| | 2 | 48 | | { |
| | 2 | 49 | | if (num < secondSmallestNumberWithRemainderOne) |
| | 1 | 50 | | { |
| | 1 | 51 | | secondSmallestNumberWithRemainderOne = num; |
| | 1 | 52 | | } |
| | | 53 | | |
| | 2 | 54 | | break; |
| | | 55 | | } |
| | 3 | 56 | | case 2 when num < smallestNumberWithRemainderTwo: |
| | 2 | 57 | | secondSmallestNumberWithRemainderTwo = smallestNumberWithRemainderTwo; |
| | 2 | 58 | | smallestNumberWithRemainderTwo = num; |
| | | 59 | | |
| | 2 | 60 | | break; |
| | | 61 | | case 2: |
| | 1 | 62 | | { |
| | 1 | 63 | | if (num < secondSmallestNumberWithRemainderTwo) |
| | 1 | 64 | | { |
| | 1 | 65 | | secondSmallestNumberWithRemainderTwo = num; |
| | 1 | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | break; |
| | | 69 | | } |
| | | 70 | | } |
| | 11 | 71 | | } |
| | | 72 | | |
| | 3 | 73 | | var sumRemainder = sum % 3; |
| | | 74 | | |
| | 3 | 75 | | switch (sumRemainder) |
| | | 76 | | { |
| | | 77 | | case 0: |
| | 0 | 78 | | return sum; |
| | | 79 | | case 1: |
| | 1 | 80 | | { |
| | 1 | 81 | | var remove2 = smallestNumberWithRemainderTwo != int.MaxValue && |
| | 1 | 82 | | secondSmallestNumberWithRemainderTwo != int.MaxValue |
| | 1 | 83 | | ? smallestNumberWithRemainderTwo + secondSmallestNumberWithRemainderTwo |
| | 1 | 84 | | : int.MaxValue; |
| | | 85 | | |
| | 1 | 86 | | return sum - Math.Min(smallestNumberWithRemainderOne, remove2); |
| | | 87 | | } |
| | | 88 | | default: |
| | 2 | 89 | | { |
| | 2 | 90 | | var remove2 = smallestNumberWithRemainderOne != int.MaxValue && |
| | 2 | 91 | | secondSmallestNumberWithRemainderOne != int.MaxValue |
| | 2 | 92 | | ? smallestNumberWithRemainderOne + secondSmallestNumberWithRemainderOne |
| | 2 | 93 | | : int.MaxValue; |
| | | 94 | | |
| | 2 | 95 | | return sum - Math.Min(smallestNumberWithRemainderTwo, remove2); |
| | | 96 | | } |
| | | 97 | | } |
| | 3 | 98 | | } |
| | | 99 | | } |