Hand of Straights
Check whether a hand of cards splits into equal-size groups of consecutive values.
The problem
You get the card values in hand and a number groupSize. Return true if you can split all the cards into groups of exactly groupSize cards, where each group holds consecutive values such as 4, 5, 6.
Input hand = [5, 3, 4, 7, 6, 8], groupSize = 3
Output true
[3, 4, 5] and [6, 7, 8].
Input hand = [2, 2, 3, 4], groupSize = 2
Output false
Both 2s must start a pair and each needs a 3, but there is only one 3.
Key constraint: Card values go up to 10^9, so count them with a hash map or a sorted map, not an array indexed by value.
How to spot the pattern
- You must use every item, in groups of consecutive values.
- The smallest item left has only one possible role, which removes all guessing.
The idea
Look at the smallest card left: nothing smaller is available, so it can't sit in the middle or at the end of a group and must start one. So if the smallest value has need copies, need groups start there, and each of the next groupSize - 1 values must have at least need copies too. Remove them and repeat with the next smallest value; if any value runs short, the answer is false.
Brute force: Sort the cards and repeatedly remove a run that starts at the smallest card from a plain list, which takes O(n^2) time because each removal scans the list.
Step by step
- 1If the number of cards is not a multiple of
groupSize, return false. - 2Count each value, and go through the distinct values in increasing order.
- 3For a value
cardwithneedcopies left (skip it whenneedis 0), look at every value fromcardtocard + groupSize - 1. - 4If any of them has fewer than
needcopies, return false; otherwise subtractneedfrom each. - 5Return true once every value has been handled.
See the pattern move
The greedy pattern on a small example. Step through it, or press play.
Code
from collections import Counter
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
if len(hand) % groupSize != 0:
return False
count = Counter(hand) # card -> copies not yet placed in a group
for card in sorted(count):
need = count[card]
if need == 0:
continue
# The smallest card left can only start a group, so `need` groups must
# start here, and each one needs card, card + 1, ..., card + groupSize - 1.
for nxt in range(card, card + groupSize):
if count[nxt] < need:
return False
count[nxt] -= need
return TrueComplexity
Time
O(n log n)
Space
O(n)
Sorting the distinct values dominates. Each start walks groupSize values, and there are at most n / groupSize starts with copies left, so that part is O(n).
Watch out for
- Starting groups at any card instead of the smallest one left, which can strand cards that had a valid place.
- Forgetting duplicates: every copy of the smallest value left starts its own group, so the next values need that many copies too.
Follow-up questions
Have you seen this problem under another name?
Divide Array in Sets of K Consecutive Numbers is the same problem, and the same greedy solves it.
What if a group could be any length of at least 3?
Then each card either extends a run that ends just below it or starts a new run. Prefer extending, and start a new run only if the next two values are available; that is Split Array into Consecutive Subsequences.
In Hinglish
Sabse chhota card kisi group ke beech mein nahi aa sakta, use group shuru karna hi padega. Uske jitne copies hain, utne groups banao aur agle values se utne hi ghatao.