AAlgoLoopSpaced repetition for LeetCode
MEDIUMGreedyLeetCode ↗

Hand of Straights

The key idea

The smallest remaining card MUST begin a group, because nothing smaller exists to lead it. Greedily start a consecutive run of length groupSize at that smallest value; if any required next card is missing, the answer is impossible.

Problem

Alice has a hand of cards, given as an array of integers. She wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards. Given the integer array hand and the integer groupSize, return true if she can rearrange the cards, or false otherwise.

Constraints

Examples

Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3 Output: true
Input: hand = [1,2,3,4,5], groupSize = 4 Output: false

Complexity

Time: O(n log n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More Greedy problems