Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Greedy

Hand of Straights

MEDIUM✓ CHIP-TIMEDLC #846 — FULL STATEMENT

The briefingthe full drill, in my own words

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.

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.

  • the hand size must divide evenly by the group size, or grouping fails
  • card values can repeat across the hand, within reasonable numeric ranges
  • every group is a run of consecutive values with no duplicate inside it
  • every card in the hand must be used — none may be left ungrouped
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 hintstake 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 boardthe 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
1 → 1
2 → 1
3 → 1
4 → 1
5 → 1
6 → 1
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 splitstwo paces, two languages

grind/hand-of-straights.py — RACE 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-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED