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
1 <= hand.length <= 10^40 <= hand[i] <= 10^91 <= groupSize <= hand.length
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
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Greedy problems
- Assign CookiesEASY
- Best Time to Buy and Sell Stock IIMEDIUM
- Boats to Save PeopleMEDIUM
- Can Place FlowersEASY
- CandyHARD
- Dota2 SenateMEDIUM
- Gas StationMEDIUM
- Increasing Triplet SubsequenceMEDIUM
- Jump GameMEDIUM
- Jump Game IIMEDIUM
- Lemonade ChangeEASY
- Longest Happy StringMEDIUM