◀ THE GRIND — GREEDY

Hand of Straights

MEDIUM✓ CHIP-TIMEDLC #846 — FULL STATEMENT ↗

The drill: A hand of cards must be dealt entirely into equal-size groups, each group a run of consecutive values with no gaps and no repeats inside it. Decide whether such a full dealing is possible.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A hand of number cards needs to be split entirely into groups of a fixed size, with nothing left over. Each group must form an unbroken run of consecutive values — no gaps, and no value repeated within the same group.

Every single card in the hand has to end up in exactly one group; cards can't be discarded or held back, and a card's value can repeat across the whole hand as long as no two copies land in the same group.

The verdict needed is simply whether such a complete grouping exists for the given hand and group size — not what the groups actually look like.

EX 01
hand = [3, 4, 2, 5, 6, 1] · groupSize = 3
true
TWO CLEAN CONSECUTIVE TRIPLES
EX 02
hand = [1, 2, 3, 4, 5] · groupSize = 4
false
HAND SIZE NOT DIVISIBLE BY GROUPSIZE
EX 03
hand = [1, 2, 3, 3, 4, 5] · groupSize = 3
true
A DUPLICATE FEEDS TWO OVERLAPPING GROUPS
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

The smallest remaining card has nowhere else to go — it can only ever be the START of a group, since no smaller card exists to extend below it. That fixes every group's first move.

HINT 2 THE STRUCTURE

So the strategy isn't really a choice: always start the next group at whatever card is currently smallest, and require the next groupSize−1 consecutive values to exist. Track how many of each value remain.

HINT 3 ONE STEP FROM THE ANSWER

Count every value; walk values from smallest to largest, and whenever a value still has cards left, consume groupSize copies of it and each of the next groupSize−1 values immediately. Any missing value fails the whole hand.

COACH'S BOARD — THE PATTERN, STEP BY STEP
SMALLEST FIRSTPATTERN · GREEDY — COUNT MAPhand = [3, 4, 2, 5, 6, 1] · groupSize = 3
1
2
3
4
5
6
COUNTS REMAINING
11
21
31
41
51
61
STEP 1

Hand [3,4,2,5,6,1], groupSize 3. Sorted, every value's count starts at 1 — walk smallest to largest.

STEP 1 / 6 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/hand-of-straights.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
        if len(hand) % groupSize != 0:
            return False
        count = collections.Counter(hand)
        for card in sorted(count):
            need = count[card]
            if need <= 0:
                continue
            for k in range(card, card + groupSize):
                if count[k] < need:
                    return False
                count[k] -= need
        return True
TIME O(N LOG N)SPACE O(N)PYTHON · RACE PACE · 14 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED