-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy path1296-DivideArrayInSetsOfKConsecutiveNumbers.cs
More file actions
41 lines (36 loc) · 1.17 KB
/
1296-DivideArrayInSetsOfKConsecutiveNumbers.cs
File metadata and controls
41 lines (36 loc) · 1.17 KB
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: 592ms
// Memory Usage: 41.2 MB
// Link: https://leetcode.com/submissions/detail/368883713/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1296_DivideArrayInSetsOfKConsecutiveNumbers
{
public bool IsPossibleDivide(int[] nums, int k)
{
if (nums.Length % k != 0) return false;
var counts = new SortedDictionary<int, int>();
foreach (var num in nums)
{
if (counts.ContainsKey(num))
counts[num]++;
else
counts[num] = 1;
}
while (counts.Count > 0)
{
var start = counts.Keys.First();
for (int i = start; i < start + k; i++)
{
if (!counts.ContainsKey(i)) return false;
if (counts[i] == 1) counts.Remove(i);
else counts[i]--;
}
}
return true;
}
}
}