-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1399-CountLargestGroup.cs
43 lines (38 loc) · 1.11 KB
/
1399-CountLargestGroup.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
//-----------------------------------------------------------------------------
// Runtime: 48ms
// Memory Usage: 15.6 MB
// Link: https://leetcode.com/submissions/detail/334988973/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1399_CountLargestGroup
{
public int CountLargestGroup(int n)
{
var groups = new Dictionary<int, int>();
for (int i = 1; i <= n; i++)
{
int sum = 0;
int num = i;
while (num != 0)
{
sum += num % 10;
num /= 10;
}
if (!groups.ContainsKey(sum))
groups[sum] = 0;
groups[sum]++;
}
var maxSize = groups.Max(p => p.Value);
int result = 0;
foreach (var g in groups)
{
if (g.Value == maxSize)
result++;
}
return result;
}
}
}