Home
DSA Patterns
Greedy Algorithms
Hand of Straights
Hand of Straights (LeetCode 846)
Hand of Straights (LeetCode 846): decide whether cards can be split into groups of consecutive values: the smallest card left must start a group, so build groups greedily from it.
Problem Understanding
Hand of Straights: given card values hand and a groupSize, return true if the cards can be rearranged into groups of groupSize consecutive values, using every card exactly once.
Example: hand = [1, 2, 3, 6, 2, 3, 4, 7, 8], groupSize = 3 returns true: [1, 2, 3], [2, 3, 4], [6, 7, 8].
The Intuition: The Smallest Card Has No Choice
Look at the smallest card left. It has to be in some group, and a group is a run of consecutive values, so the group must start with it. Nothing about that is a guess, which is why a greedy pass is correct.
- If
len(hand)is not a multiple ofgroupSize, returnfalse. - Count every value and walk the distinct values from smallest to largest.
- If
needcopies of the current value are left,needgroups start here. Each of the nextgroupSize − 1values must have at leastneedcopies; take them. - If any value runs short, return
false. If the walk finishes, returntrue.
The counting is the same idea as Top K Frequent Elements: a map from value to how many are left.
Hand of Straights Visualization
GROUPS OF 3 CONSECUTIVE CARDS · COLUMNS ARE CARD VALUES
COUNT THE CARDS
1
2
3
4
6
7
8
left
start
:
groups
0
Answer
:
groups formed
None yet
Count each card value. 9 cards make 3 groups of 3
Next
1 is the smallest card left, so 1 group must start here: 1..3
hand
groupSize
Each column is a card value with the number of copies still unplaced. The smallest value with copies left is outlined: groups start there. Watch the counts of the following values drop as each group is completed.
Hand of Straights Dry Run
Input: [1, 2, 3, 6, 2, 3, 4, 7, 8], groupSize = 3. Counts: 1×1, 2×2, 3×2, 4×1, 6×1, 7×1, 8×1.
| Smallest left | Groups started | Takes | Counts after |
|---|---|---|---|
| 1 | 1 | 1, 2, 3 | 2×1, 3×1, 4×1, 6, 7, 8 |
| 2 | 1 | 2, 3, 4 | 6×1, 7×1, 8×1 |
| 3, 4 | 0 | - | skipped |
| 6 | 1 | 6, 7, 8 | all 0 |
Every card is used: true.
Hand of Straights LeetCode Solution
Sorted counts, then greedy groups from the smallest value. The C snippet sorts the cards and builds one group at a time with a used flag.
Hand of Straights Code
1var isNStraightHand = function(hand, groupSize) {2 if (hand.length % groupSize) return false;3 const count = new Map();4 for (const card of hand) count.set(card, (count.get(card) || 0) + 1);5 for (const start of [...count.keys()].sort((a, b) => a - b)) {6 const need = count.get(start);7 if (need === 0) continue;8 for (let card = start; card < start + groupSize; card++) {9 if ((count.get(card) || 0) < need) return false;10 count.set(card, count.get(card) - need);11 }12 }13 return true;14};
Hand of Straights in Python, Java, C++, TypeScript and C
Hand of Straights in Python
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
if len(hand) % groupSize:
return False
count = Counter(hand)
for start in sorted(count):
need = count[start]
if need == 0:
continue
for card in range(start, start + groupSize):
if count[card] < need:
return False
count[card] -= need
return TrueHand of Straights in Java
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length % groupSize != 0) return false;
TreeMap<Integer, Integer> count = new TreeMap<>();
for (int card : hand) count.merge(card, 1, Integer::sum);
for (int start : count.keySet()) {
int need = count.get(start);
if (need == 0) continue;
for (int card = start; card < start + groupSize; card++) {
if (count.getOrDefault(card, 0) < need) return false;
count.put(card, count.get(card) - need);
}
}
return true;
}
}Hand of Straights in C++
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
if (hand.size() % groupSize) return false;
map<int, int> count;
for (int card : hand) count[card]++;
for (auto& [start, freq] : count) {
int need = freq;
if (need == 0) continue;
for (int card = start; card < start + groupSize; card++) {
auto it = count.find(card);
if (it == count.end() || it->second < need) return false;
it->second -= need;
}
}
return true;
}
};Hand of Straights in TypeScript
function isNStraightHand(hand: number[], groupSize: number): boolean {
if (hand.length % groupSize) return false;
const count = new Map<number, number>();
for (const card of hand) count.set(card, (count.get(card) || 0) + 1);
for (const start of [...count.keys()].sort((a, b) => a - b)) {
const need = count.get(start)!;
if (need === 0) continue;
for (let card = start; card < start + groupSize; card++) {
if ((count.get(card) || 0) < need) return false;
count.set(card, count.get(card)! - need);
}
}
return true;
}Hand of Straights in C
static int cmp(const void* a, const void* b) { return *(int*)a - *(int*)b; }
bool isNStraightHand(int* hand, int handSize, int groupSize) {
if (handSize % groupSize) return false;
qsort(hand, handSize, sizeof(int), cmp);
bool* used = calloc(handSize, sizeof(bool));
for (int i = 0; i < handSize; i++) {
if (used[i]) continue;
int want = hand[i], j = i;
for (int k = 0; k < groupSize; k++, want++) {
while (j < handSize && (used[j] || hand[j] < want)) j++;
if (j == handSize || hand[j] != want) { free(used); return false; }
used[j] = true;
}
}
free(used);
return true;
}Edge Cases & Common Mistakes
- Size not a multiple: return
falsebefore counting. - groupSize = 1: every card is its own group: always
true. - Gaps in values:
count[card]for a missing value is 0, which correctly fails. - Starting groups at any card: only the smallest remaining card is a safe start; starting elsewhere can strand smaller cards.
Summary
The smallest card left must start a group. Count the values, walk them in increasing order, and for each value with need copies left, take need copies of each of the next groupSize − 1 values; fail if any runs short. O(n log n) for the sorted keys.
More practice: DSA interview questions with answers · patterns cheat sheet
