-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1262.cs
51 lines (40 loc) · 1.3 KB
/
Solution1262.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1262
{
/// <summary>
/// 1262. Greatest Sum Divisible by Three - Medium
/// <a href="https://leetcode.com/problems/greatest-sum-divisible-by-three">See the problem</a>
/// </summary>
public int MaxSumDivThree(int[] nums)
{
int totalSum = 0;
var mod1 = new List<int>();
var mod2 = new List<int>();
foreach (var num in nums)
{
totalSum += num;
if (num % 3 == 1) mod1.Add(num);
else if (num % 3 == 2) mod2.Add(num);
}
mod1.Sort();
mod2.Sort();
if (totalSum % 3 == 0)
return totalSum;
int result = 0;
if (totalSum % 3 == 1)
{
int remove1 = mod1.Count >= 1 ? mod1[0] : int.MaxValue;
int remove2 = mod2.Count >= 2 ? mod2[0] + mod2[1] : int.MaxValue;
result = totalSum - Math.Min(remove1, remove2);
}
else if (totalSum % 3 == 2)
{
int remove1 = mod2.Count >= 1 ? mod2[0] : int.MaxValue;
int remove2 = mod1.Count >= 2 ? mod1[0] + mod1[1] : int.MaxValue;
result = totalSum - Math.Min(remove1, remove2);
}
return result < 0 ? 0 : result;
}
}