Skip to main content

Hand of Straights

Medium
Data Structures and Algorithms
Editorial
Topics:
Array
Greedy
Hash Table

You are given hand, where each integer represents one card value, and groupSize, the required number of cards in each group. Partition every card in hand into groups of exactly groupSize cards.

A group is valid only when its card values are consecutive integers, with no gaps. Cards with the same value are distinct occurrences, so duplicate values may be used in different groups. Return true if such a partition is possible; otherwise, return false.

The order of cards in hand does not matter, and every card must belong to exactly one group.

Example 1:

Input: hand = [11,12,13,21,22,23], groupSize = 3
Output: true
Explanation: The cards can be partitioned into [11, 12, 13] and [21, 22, 23], two groups of three consecutive values.

Example 2:

Input: hand = [14,15,17,18,19,20], groupSize = 3
Output: false
Explanation: Although the hand has six cards, the smallest possible group would require 16 after 14 and 15, but no such card is available.

Example 3:

Input: hand = [20,21,22,23,21,22,23,24], groupSize = 4
Output: true
Explanation: One valid partition is [20, 21, 22, 23] and [21, 22, 23, 24].

Constraints:

  • 1 <= hand.length <= 10^4
  • 0 <= hand[i] <= 10^9
  • 1 <= groupSize <= hand.length
  • All values in hand are integers

Need help or want more practice?

Practise in an interviewGet AI Coach help