Hand of Straights
Decide whether a hand of cards can be split into runs of consecutive values, all of the same length.
Given a hand of cards as an array of integers handgroupSizegroupSizetruefalse
Example 1
Output: true
Explanation: the cards form [1, 2, 3], [2, 3, 4] and [6, 7, 8].
Example 2
Output: false
Explanation: 5 cards cannot be split into groups of 4.
The Idea
In card games, a run of cards whose values follow one another, like 6, 7, 8, is called a straight. This problem hands us a pile of cards and asks whether all of them can be laid out as straights of the same length, with no card left over.
Take Example 1. Its nine cards, sorted, are 1, 2, 2, 3, 3, 4, 6, 7, 8, and each straight must have three cards. The 2 and the 3 appear twice, so each copy could join a different straight. Which straight should each card join? The number of ways to share out the cards grows quickly as the hand gets bigger.
Look at the card 1 instead. No card in the hand is smaller, so 1 cannot sit in the middle or at the end of a straight: nothing could come before it. Card 1 has to start a straight, and that straight must be 1, 2, 3. There is no choice to make.
Once 1, 2 and 3 are put aside, the same reasoning works for the cards that are left. Look at the picture below.
The orange card in each row is the smallest card left, the blue cards complete its straight, and the dashed grey cards are already used. In the row labelled "round 2", the smallest card left is the second 2, so it starts 2, 3, 4. In the row labelled "round 3", only 6, 7 and 8 remain, and 6 starts the last straight. Every card is used, so the answer is true
The same rule also shows when the answer is false
Building the answer one straight at a time, each time from the smallest card left, and never going back to change a straight already laid out, is called a greedy approach. It works here because the smallest card's straight is forced, so no other choice could have done better.
Solution Approach
Turning that idea into code means settling three things: a quick check on the number of cards, how to find the smallest card left, and how to take one copy of each card in a straight.
Counting the cards first
Can some hands be ruled out at once? Every straight has groupSizegroupSizefalse
Finding the smallest card left
How do we find the smallest card left, round after round? The plain way is to scan the whole hand each time a straight starts. With many short straights, that repeats a scan of every card once per straight.
A better way keeps the cards in a map from card value to how many copies of it are left. For Example 1 the map is {1=1, 2=2, 3=2, 4=1, 6=1, 7=1, 8=1}TreeMapfirstKey()
Taking one copy of each card
What happens to the map when a straight is laid out? For each of the groupSizefalse
When a value's last copy is taken, its entry is removed rather than set to 0. Otherwise firstKey()
Java Solution
The method isNStraightHand
- First, check
. If it is not 0, the cards cannot fill whole straights, so returnhand.length % groupSize .false - Build
, acount from card value to number of copies, by callingTreeMap for each card in the hand.count.merge(card, 1, Integer::sum) - While
is not empty, read the smallest card left withcount intocount.firstKey() .start - Inside, a second loop moves
fromcard up tostart . For each value it reads the number of copies,start + groupSize - 1 , withc . Ifcount.get(card) isc , the value is missing and the method returnsnull . Iffalse is 1, the entry is removed; otherwise it becomesc .c - 1 - When
is empty, every card is in a straight, so returncount .true
The main
import java.util.TreeMap;
public class HandOfStraights {
public static boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length % groupSize != 0) {
return false; // the cards cannot fill whole straights
}
TreeMap<Integer, Integer> count = new TreeMap<>();
for (int card : hand) {
count.merge(card, 1, Integer::sum);
}
while (!count.isEmpty()) {
int start = count.firstKey(); // the smallest card must start a straight
for (int card = start; card < start + groupSize; card++) {
Integer c = count.get(card);
if (c == null) {
return false; // the straight would have a gap
}
if (c == 1) {
count.remove(card); // last copy: drop the value
} else {
count.put(card, c - 1);
}
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isNStraightHand(new int[] {1, 2, 3, 6, 2, 3, 4, 7, 8}, 3));
System.out.println(isNStraightHand(new int[] {1, 2, 3, 4, 5}, 4));
System.out.println(isNStraightHand(new int[] {1, 3, 4, 5}, 2));
System.out.println(isNStraightHand(new int[] {1, 1, 2, 2}, 2));
System.out.println(isNStraightHand(new int[] {2, 1, 3, 4}, 2));
System.out.println(isNStraightHand(new int[] {5, 9, 7}, 1));
}
}
Running it prints:
true
false
false
true
true
true
Example 1 gives truefalse[1, 3, 4, 5]false
The fourth line, for [1, 1, 2, 2]true
The fifth line, for [2, 1, 3, 4]truefalsegroupSizetrue
Complexity
The work is one map update for each card when the map is built, and one more when that card goes into a straight.
- Time: O(n log n), because each of the n cards costs one
to count it and onemerge plus aget orput to place it, and eachremove call takes O(log n) steps on a map of at most n entries.TreeMap - Space: O(n), because the map holds one entry for each different card value, which is n entries when no two cards are equal.
Starting from the smallest item, because its role is forced, also drives