TreeMap counts
Time O(n log n) Space O(n)While cards remain: first = smallest key; for v in first..first + size − 1, decrement (fail if missing).
import java.util.*;
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length % groupSize != 0) return false;
TreeMap<Integer, Integer> count = new TreeMap<>();
for (int c : hand) count.merge(c, 1, Integer::sum);
while (!count.isEmpty()) {
int first = count.firstKey();
for (int v = first; v < first + groupSize; v++) {
Integer c = count.get(v);
if (c == null) return false;
if (c == 1) count.remove(v); else count.put(v, c - 1);
}
}
return true;
}
}Verdict: The forced smallest choice is the proof.