Alice has some number of cards, and she wants to rearrange them into groups so that each group has group_size cards and consists of group_size consecutive values.
Given an integer array hand, where hand[i] is the value written on the i-th card, and an integer group_size, return True if she can rearrange the cards this way, or False otherwise.
Function Signature
def is_n_straight_hand(hand: List[int], group_size: int) -> bool:
Rules
-
Every card goes into exactly one group, and every group has exactly
group_size
cards.
-
A group is consecutive when its values, sorted, are
x, x + 1, ..., x + group_size - 1
for some
x
.
-
Cards with the same value are separate cards.
Constraints
-
1 <= len(hand) <= 10^4
-
0 <= hand[i] <= 10^9
-
1 <= group_size <= len(hand)
Examples
Input: hand = [1,2,3,6,2,3,4,7,8], group_size = 3
Output: True
Sorted, the cards are 1, 2, 2, 3, 3, 4, 6, 7, 8. They split into [1,2,3], [2,3,4] and [6,7,8], which uses all nine cards.
Input: hand = [1,2,3,4,5], group_size = 4
Output: False
Five cards cannot form groups of 4, because 5 is not a multiple of 4.
Input: hand = [1,1,2,3], group_size = 2
Output: False
There is no card 0, so each card 1 can only go into the group [1,2]. Two such groups need two cards of value 2, and the hand has one.