Hand of Straights
Master this topic with zero to advance depth.
Hand of Straights
Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.
Given an integer array hand and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
Visual Representation
Hand: [1,2,3,6,2,3,4,7,8], Size: 3
1. Smallest is 1. Start group: [1, 2, 3]. Remaining: [6, 2, 3, 4, 7, 8]
2. Smallest is 2. Start group: [2, 3, 4]. Remaining: [6, 7, 8]
3. Smallest is 6. Start group: [6, 7, 8]. Remaining: []
Result: TrueExamples
Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Level I: Recursive Backtracking
Intuition
Try all possible ways to group the cards. For each card, either start a new group or add it to an existing incomplete group. This is highly inefficient but follows the core requirement.
Thought Process
solve(remaining_cards): Pick a card and try to form a group ofgroupSizestarting with it.- Recurse with the remaining cards.
- If all cards are used, return
true.
Pattern: Brute Force / Backtracking
Detailed Dry Run
Hand: [1,2,3,4], Size: 2
- Try [1,2], left [3,4].
- Try [3,4], left []. Success.
Level II: Sorting + Simulation (O(N^2))
Intuition
Sort the hand first. Then, repeatedly find the smallest card and try to form a group of size groupSize starting from it by searching for subsequent cards in the array.
Thought Process
- Sort
hand. - Use a boolean array
usedto mark cards. - For each
ifrom0ton-1:- If
!used[i]:- Mark
used[i] = true. This is the start of a group. - Search for
hand[i]+1, hand[i]+2, ..., hand[i]+groupSize-1in the remaining array. - If any card is not found or already used, return
false.
- Mark
- If
Pattern: Sorting and Linear Scanning
Detailed Dry Run
Hand: [1,2,2,3,3,4], Size: 3. Sorted: [1,2,2,3,3,4]
- Start with 1. Find 2 (idx 1) and 3 (idx 3). Mark used. [X, X, 2, X, 3, 4]
- Next unused is 2. Find 3 (idx 4) and 4 (idx 5). Mark used. [X, X, X, X, X, X] Success.
Level III: Optimal Greedy (TreeMap/Frequency Map)
Intuition
To form consecutive groups, every 'start' of a group must be the smallest available card. If we always start groups with the smallest remaining card, we either succeed or hit a card that cannot complete a group.
Thought Process
- Count frequencies of all cards.
- Sort unique cards (using a TreeMap or sorted keys).
- For each card that still has remaining counts:
- It MUST be the start of
count[C]groups. - Each such group requires cards .
- If any of these cards have fewer counts than
count[C], returnfalse. - Decrement counts of all cards in the group.
- It MUST be the start of
Pattern: Greedy Frequency Grouping
Detailed Dry Run
Hand: [1,2,2,3,3,4], Size: 3 Map: {1:1, 2:2, 3:2, 4:1}
- Smallest is 1. Count=1. Start group [1,2,3]. Map becomes: {1:0, 2:1, 3:1, 4:1}
- Next smallest is 2. Count=1. Start group [2,3,4]. Map becomes: {2:0, 3:0, 4:0} Result: True.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.