You get a list hand and a number groupSize. Each number in hand is the value of one card.
Return True if you can split all the cards into groups of consecutive values. Return False if you can't.
Each group has exactly groupSize cards. Their values follow each other with no gaps, like 4, 5 and 6.
Every card goes into exactly one group. The order of the cards in hand doesn't matter.
Example 1:
Input: hand = [5,7,6,10,12,11], groupSize = 3 Output: True Explanation: The groups are [5,6,7] and [10,11,12].
Example 2:
Input: hand = [2,3,3,4,4,5], groupSize = 3 Output: True Explanation: The two 3s go into different groups. The groups are [2,3,4] and [3,4,5].
Example 3:
Input: hand = [1,2,2,3,4,6], groupSize = 3 Output: False Explanation: The 1 can only go into [1,2,3]. The cards left are 2, 4 and 6. They have gaps between them, so they can't form a group.
Constraints:
- 1 to 10,000 cards.
- Each card value is a whole number from 0 to 1,000,000,000.
groupSizeis from 1 to the number of cards.
Note:
Your solution should run in O(n log n) time.
Free plan: 30 tutor messages. Premium is unlimited.
