Hand of Straights

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given an array of integers hand where hand[i] is the value of the i-th card, and an integer groupSize, return true if and only if the hand can be rearranged into groups of groupSize consecutive cards.

Examples

Example 1:

Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3

Output: true

Explanation: The hand can be rearranged as [1,2,3], [2,3,4], [6,7,8].

Example 2:

Input: hand = [1,2,3,4,5], groupSize = 4

Output: false

Explanation: The hand cannot be rearranged into groups of 4 consecutive cards.

Example 3:

Input: hand = [1,2,3,4,5,6], groupSize = 2

Output: true

Explanation: The hand can be rearranged as [1,2], [3,4], [5,6].

Constraints

  • 1 <= hand.length <= 10^4
  • 0 <= hand[i] <= 10^9
  • 1 <= groupSize <= hand.length
Source: Greedy Techniques pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle