Algorithm


Problem Name: 846. Hand of Straights

Problem Link: https://leetcode.com/problems/hand-of-straights/


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.

 

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: hand = [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 <= 104
  • 0 <= hand[i] <= 109
  • 1 <= groupSize <= hand.length

 
 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int W) {
        int n = hand.size();
        if(n % W) return false;
        map < int, int>m;
        for(int x: hand) m[x]++;
        auto l = m.begin();
        for(int i = 0; i  <  n/W; i++){
            auto r = l, t = m.end();
            advance(r, W - 1);
            for(int j = W - 1; j >= 0; j--, r--)
                if((*r).second-- <= 0 || ((*r).first - (*l).first != j)) return false;
                else if((*r>.second > 0) t = r;
            if(t != m.end()) l = t;
            else advance(l, W);
        }
        return true;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
hand = [1,2,3,6,2,3,4,7,8], groupSize = 3

Output

x
+
cmd
true

#2 Code Example with Java Programming

Code - Java Programming

start coding...
class Solution {
  public boolean isNStraightHand(int[] hand, int W) {
    if (hand.length % W != 0) {
      return false;
    }
    Map < Integer, Integer> counterMap = new HashMap<>();
    Set set = new TreeSet<>();
    for (int num : hand) {
      counterMap.put(num, counterMap.getOrDefault(num, 0) + 1);
      set.add(num);
    }
    Iterator < Integer> iter = set.iterator();
    Integer curr = iter.next();
    while (counterMap.get(curr) > 0) {
      int temp = curr;
      for (int i = 0; i  <  W; i++) {
        if (counterMap.getOrDefault(temp, 0) == 0) {
          return false;
        }
        counterMap.put(temp, counterMap.get(temp) - 1);
        temp++;
      }
      while (iter.hasNext() && counterMap.get(curr) == 0) {
        curr = iter.next();
      }
    }
    return true;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
hand = [1,2,3,6,2,3,4,7,8], groupSize = 3

Output

x
+
cmd
true

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def isNStraightHand(self, hand, W):
        hand.sort()
        while hand:
            try:
                base = hand[0]
                for i in range(W):
                    hand.remove(base+i)
            except:
                return False
        return True
Copy The Code & Try With Live Editor

Input

x
+
cmd
hand = [1,2,3,4,5], groupSize = 4

Output

x
+
cmd
false

#4 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;
using System.Linq;

namespace LeetCode
{
    public class _0846_HandOfStraights
    {
        public bool IsNStraightHand(int[] hand, int W)
        {
            if (hand.Length % W != 0) return false;

            var counts = new SortedDictionary < int, int>();
            foreach (var num in hand)
            {
                if (counts.ContainsKey(num))
                    counts[num]++;
                else
                    counts[num] = 1;
            }

            while (counts.Count > 0)
            {
                var start = counts.Keys.First();
                for (int i = start; i  <  start + W; i++)
                {
                    if (!counts.ContainsKey(i)) return false;
                    if (counts[i] == 1) counts.Remove(i);
                    else counts[i]--;
                }
            }

            return true;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
hand = [1,2,3,4,5], groupSize = 4

Output

x
+
cmd
false
Advertisements

Demonstration


Previous
#845 Leetcode Longest Mountain in Array Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#847 Leetcode Shortest Path Visiting All Nodes Solution in C, C++, Java, JavaScript, Python, C# Leetcode