-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0846-HandOfStraights.cs
41 lines (36 loc) · 1.14 KB
/
0846-HandOfStraights.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: 264ms
// Memory Usage: 33 MB
// Link: https://leetcode.com/submissions/detail/368886996/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _0846_HandOfStraights
{
public bool IsNStraightHand(int[] hand, int W)
{
if (hand.Length % W != 0) return false;
var counts = new SortedDictionary<int, int>();
foreach (var num in hand)
{
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 + W; i++)
{
if (!counts.ContainsKey(i)) return false;
if (counts[i] == 1) counts.Remove(i);
else counts[i]--;
}
}
return true;
}
}
}