LeetCode 846: Hand of Straights Solution

Master LeetCode problem 846 (Hand of Straights), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

846. Hand of Straights

Problem Explanation

Explanation:

To solve this problem, we can use a hashmap to count the frequency of each card value in the hand. Then, we can iterate over the keys of the hashmap in sorted order. For each card value, we check if there are enough cards available to form a group of size groupSize. If so, we decrement the count of the current card value and the counts of the consecutive cards in the group. If at any point we find that we cannot form a group, we return false. If we are able to form groups for all card values, we return true.

Algorithm:

  1. Create a hashmap to store the frequency of each card value in the hand.
  2. Sort the keys of the hashmap.
  3. For each card value in sorted order:
    • If the count of the current card value is greater than 0:
      • Decrement the count of the current card value.
      • Decrement the counts of the consecutive cards in the group.
      • If we cannot form a group, return false.
  4. If we are able to form groups for all card values, return true.

Time Complexity: O(n log n) where n is the number of cards in the hand.

Space Complexity: O(n) where n is the number of cards in the hand.

:

Solution Code

class Solution {
    public boolean isNStraightHand(int[] hand, int groupSize) {
        if (hand.length % groupSize != 0) {
            return false;
        }
        
        TreeMap<Integer, Integer> countMap = new TreeMap<>();
        for (int card : hand) {
            countMap.put(card, countMap.getOrDefault(card, 0) + 1);
        }
        
        for (int card : countMap.keySet()) {
            int count = countMap.get(card);
            if (count > 0) {
                for (int i = 0; i < groupSize; i++) {
                    if (countMap.getOrDefault(card + i, 0) < count) {
                        return false;
                    }
                    countMap.put(card + i, countMap.get(card + i) - count);
                }
            }
        }
        
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 846 (Hand of Straights)?

This page provides optimized solutions for LeetCode problem 846 (Hand of Straights) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 846 (Hand of Straights)?

The time complexity for LeetCode 846 (Hand of Straights) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 846 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 846 in Java, C++, or Python.

Back to LeetCode Solutions