-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1282-GroupThePeopleGivenTheGroupSizeTheyBelongTo.cs
41 lines (37 loc) · 1.29 KB
/
1282-GroupThePeopleGivenTheGroupSizeTheyBelongTo.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
//-----------------------------------------------------------------------------
// Runtime: 252ms
// Memory Usage: 32.4 MB
// Link: https://leetcode.com/submissions/detail/360356852/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1282_GroupThePeopleGivenTheGroupSizeTheyBelongTo
{
public IList<IList<int>> GroupThePeople(int[] groupSizes)
{
var counts = new Dictionary<int, IList<int>>();
for (int i = 0; i < groupSizes.Length; i++)
{
if (!counts.ContainsKey(groupSizes[i]))
counts.Add(groupSizes[i], new List<int>());
counts[groupSizes[i]].Add(i);
}
var results = new List<IList<int>>();
foreach (var pair in counts)
{
var result = new List<int>(pair.Key);
foreach (var item in pair.Value)
{
result.Add(item);
if (result.Count == pair.Key)
{
results.Add(result);
result = new List<int>(pair.Key);
}
}
}
return results;
}
}
}