Alice wants to rearrange her cards into groups of groupSize consecutive values. Return true if every card can be placed in such a group.
Sort the hand. Repeatedly take the smallest unused card and pull the next k − 1 consecutive values out of the remaining cards; fail if one is missing.
1function isNStraightHand(hand: number[], k: number): boolean {2if (hand.length % k) return false;3hand.sort((a, b) => a - b);4const used = new Array(hand.length).fill(false);5for (let i = 0; i < hand.length; i++) {6if (used[i]) continue;7let want = hand[i], j = i;8for (let got = 0; got < k; got++, want++) {9while (j < hand.length && (used[j] || hand[j] < want)) j++;10if (j === hand.length || hand[j] !== want) return false;11used[j] = true;12}13}14return true;15}
Sort the hand.
Space: play/pause · ←/→: step