Hand of Straights
MediumGreedy
You are given a list hand of card values and an integer group_size. Decide whether all of the cards can be split into groups such that:
- every group has exactly
group_sizecards, and - the cards in each group are consecutive values, such as
7, 8, 9.
Every card must be used in exactly one group. Cards with the same value are separate cards. Return True if such a split exists, otherwise False.
Example 1
- Input:
- hand = [4, 6, 5, 2, 3, 7], group_size = 3
- Output:
- True
- Explanation
- Sorted, the cards are
2, 3, 4, 5, 6, 7, which split into[2, 3, 4]and[5, 6, 7].
Example 2
- Input:
- hand = [1, 2, 2, 3, 3, 4, 5, 5], group_size = 4
- Output:
- False
- Explanation
- The smallest card, 1, must start a group, so
[1, 2, 3, 4]is forced. That leaves2, 3, 5, 5, and a group starting at 2 needs a 4, which is already used.
Example 3
- Input:
- hand = [10, 11, 11, 12], group_size = 2
- Output:
- True
- Explanation
- Four cards make two pairs:
[10, 11]and[11, 12]. The two 11s are separate cards and end up in different groups.
Constraints
- 1 ≤
hand.length≤ 104 - 0 ≤
hand[i]≤ 109 - 1 ≤
group_size≤hand.length