Hand of Straights
Medium · greedy
You are given a pile of cards. Each card shows a whole number, and cards[i] is the number on card i. Different cards may show the same number.
Deal out the whole pile into groups of exactly group_size cards, so that every card is in exactly one group and the numbers in each group are consecutive: they go up by one each time, like 4, 5, 6.
Return true if the pile can be dealt out this way, and false otherwise.
Examples
Example 1
Input: cards = [3, 4, 2, 6, 5, 3, 4, 5, 4], group_size = 3
Output: true
Deal 2, 3, 4 and 3, 4, 5 and 4, 5, 6.
Example 2
Input: cards = [1, 2, 3, 5], group_size = 2
Output: false
The 5 would need a 4 or a 6 beside it in its group, and there is neither, so the cards cannot be dealt out.
Example 3
Input: cards = [7, 7, 7], group_size = 1
Output: true
Every group is a single card, and a single card is always consecutive.
Constraints
- 1 <= len(cards) <= 10^4
- 0 <= cards[i] <= 10^9
- 1 <= group_size <= len(cards)