← Whiteboard questions

Hand of Straights

Split cards into groups of consecutive ranks, true or false

DSA, Medium, Greedy

Which structure or algorithm, and what does it cost.

A hand of cards is given as the list hand, and each card shows a whole-number rank. Deal all the cards into groups of exactly size cards. The ranks in one group must be consecutive, such as 6, 7, 8. Every card goes into exactly one group. Return True if that is possible. Otherwise return False.

Examples

Input hand = [4, 5, 5, 6, 6, 7], size = 3
Output True
Why the groups 4, 5, 6 and 5, 6, 7 use every card once.
Input hand = [2, 3, 4, 4, 5, 7], size = 3
Output False
Why after the group 2, 3, 4 the cards 4, 5, 7 remain, and the rank 6 is missing.
Input hand = [9, 2, 2, 5], size = 1
Output True
Why with a size of 1 every card is a group by itself.

Constraints

Your answer must contain

Target Aim for O(n log n) time and O(n) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Think about the card with the smallest rank. Could it sit in the middle of a group?

Hint 2 the approach

Count how many cards carry each rank. Then handle the ranks from small to large, building groups as you go.

Hint 3 the key step

Take the smallest rank with cards left. Each of its cards starts a group, so each later rank in it needs as many cards. Remove them, or fail.

Walkthrough

Full walkthrough brute force to best

Brute force

Search over all ways to group. Take any unused card, try every position it could hold inside a group, remove the matching cards, and repeat. A group has size positions to try and there are n / size groups, so the search can take up to size to the power n / size steps. It blows up quickly, so it only works for tiny inputs.

Best method

Count the cards per rank and go through the distinct ranks from smallest to largest. For a rank x that still has c cards, all c of them must start a group, because no smaller card is left to hold them as a later member. So build c groups x, x + 1, up to x + size - 1. Each of those ranks must have at least c cards. Subtract c from each of them, and return False if one is short. If you finish, return True. Starting at the smallest rank is forced, not only safe, since the smallest card can only be the first of its group. First reject a card count that is not a multiple of size.

Hand trace

Run on hand [4, 5, 5, 6, 6, 7] with size 3. There are 6 cards, a multiple of 3. The counts are 4: one card, 5: two, 6: two, 7: one. Smallest rank 4 has 1 card, so build the group 4, 5, 6. Now 5 has one card, 6 has one and 7 has one. Next rank 5 has 1 card, so build the group 5, 6, 7. Every count is 0, so the answer is True.

Code

from collections import Counter
def can_deal(hand, size):
    if len(hand) % size:
        return False
    count = Counter(hand)
    for x in sorted(count):         # smallest rank first
        c = count[x]
        if c:                       # c groups must start at x
            for y in range(x, x + size):
                if count[y] < c:
                    return False
                count[y] -= c
    return True

Time and memory

O(n log n) time, because sorting the distinct ranks dominates. The group loops touch each card once in total, since every start covers at least size cards. O(n) memory for the counts.

Common mistakes

  • Going through the ranks in the order they appear. The group must start at the smallest rank that is left, so sort first.
  • Building one group when a rank has several cards. If rank x has c cards left, c groups start there.
  • Accepting ranks that skip a number, such as 4, 5, 7. Every rank from x up to x + size - 1 must have enough cards.

Related problems

More Greedy problems
Hand of Straights: hints, walkthrough and Python solution