-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution846.cs
48 lines (43 loc) · 1.13 KB
/
Solution846.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
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution846
{
/// <summary>
/// 846. Hand of Straights - Medium
/// <a href="https://leetcode.com/problems/hand-of-straights">See the problem</a>
/// </summary>
public bool IsNStraightHand(int[] hand, int groupSize)
{
if (hand.Length % groupSize != 0)
{
return false;
}
var count = new SortedDictionary<int, int>();
foreach (int card in hand)
{
if (!count.ContainsKey(card))
{
count[card] = 0;
}
count[card]++;
}
while (count.Count > 0)
{
int first = count.First().Key;
for (int card = first; card < first + groupSize; card++)
{
if (!count.ContainsKey(card))
{
return false;
}
count[card]--;
if (count[card] == 0)
{
count.Remove(card);
}
}
}
return true;
}
}