Start learning free

LESSON 511 OF 635 · GREEDY

Hand of Straights in Java

Practice O(n log n)

You get an array 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.
  • groupSize is from 1 to the number of cards.

Note:

Your solution should run in O(n log n) time.

STEP 1 OF 12
    Ctrl + Enter to check

    Hand Of Straights in Java

    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free