Skip to main content

846. Hand of Straights

Problem Description​

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.

Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.

Examples​

Example 1:

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

Output: true

Explanation:
Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]

Example 2:

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

Output: false

Explanation:
Alice's hand can not be rearranged into groups of 4.

Constraints​

  • 1 <= hand.length <= 10^4
  • 0 <= hand[i] <= 10^9
  • 1 <= groupSize <= hand.length

Solution for 846. Hand of Straights​

The problem is about arranging cards into groups of consecutive sequences. The key observation is that, to form valid groups, each smallest number in the group should lead to groupSize - 1 consecutive numbers following it. If we can always find such sequences starting from the smallest number, then it is possible to arrange the cards into the desired groups.

Approach​

  1. Sort the Cards: The first step is to sort the cards. Sorting helps us to easily identify and form consecutive sequences.

  2. Find Successors: For each number in the sorted array, if it hasn't already been used in a group (indicated by a value of -1), try to form a group starting with that number. Use a helper function findSuccessors to check if it is possible to form a valid group starting from the current number.

  3. Mark Used Cards: Once a card is used in a group, mark it as -1 to indicate it has been processed.

  4. Check Group Formation: For each number, use the helper function to see if a group of groupSize consecutive numbers can be formed. If at any point, it is not possible to form such a group, return false.

  5. Completion Check: If all numbers can be grouped correctly, return true.

Code in Different Languages​

Written by @nagalakshmi08
class Solution {
public:
bool findSuccessors(vector<int>& hand, int groupSize, int i, int n) {
int next = hand[i] + 1;
hand[i] = -1; // Mark as used
int count = 1;
i += 1;
while (i < n && count < groupSize) {
if (hand[i] == next) {
next = hand[i] + 1;
hand[i] = -1;
count++;
}
i++;
}
return count == groupSize;
}

bool isNStraightHand(vector<int>& hand, int groupSize) {
int n = hand.size();
if (n % groupSize != 0) return false;
std::sort(hand.begin(), hand.end());
for (int i = 0; i < n; i++) {
if (hand[i] >= 0) {
if (!findSuccessors(hand, groupSize, i, n)) return false;
}
}
return true;
}
};

Complexity Analysis​

  • Time Complexity: Sorting the array takes (O(nlog⁑n))(O(n \log n)). The subsequent grouping operation in the worst case can take (O(nΓ—groupSize))(O(n \times groupSize)), leading to an overall time complexity of (O(nlog⁑n+nΓ—groupSize))(O(n \log n + n \times groupSize)).
  • Space Complexity: IThe space complexity is (O(1))(O(1)) for the in-place operations (aside from the input array).

Authors:

Loading...