Hand of Straights
Medium · Greedy
You are given a list of integers representing card values held in a hand, and a target group size. Determine whether it is possible to rearrange every card into groups of that exact size, where each group consists of consecutive integers with no gaps. Every card must be used in exactly one group, and you must use all of them. Return true if such a grouping exists, otherwise return false.
Examples
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output: true
Why: The cards can be split into consecutive runs [1,2,3], [2,3,4], [6,7,8].
Input: hand = [1,2,3,4,5], groupSize = 4
Output: false
Why: Five cards cannot be evenly divided into groups of four.
Input: hand = [1,1,2,2,3,3], groupSize = 3
Output: true
Why: The cards split into [1,2,3] and [1,2,3].
Constraints
1 <= hand.length <= 10^4, 0 <= hand[i] <= 10^9, 1 <= groupSize <= hand.length
Practise it by voice
Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.
This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Hand of Straights. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.