classSolution{publicbooleanisNStraightHand(int[] hand,int groupSize){// Check if divisibleif(hand.length % groupSize !=0)returnfalse;// Count the amount of each numberMap<Integer,Integer> count =newHashMap<>();for(int i =0; i < hand.length;++i){
count.put(hand[i], count.getOrDefault(hand[i],0)+1);}// Use min heapPriorityQueue<Integer> minH =newPriorityQueue<>((a, b)->(a - b));for(int key : count.keySet()){
minH.add(key);}// Until all the numbers are pickedwhile(!minH.isEmpty()){// First number of a groupint first = minH.peek();// Try to make the group completefor(int i = first; i < first + groupSize;++i){// Must contain the number we needif(!count.containsKey(i))returnfalse;// Decrease the amount of number i
count.put(i, count.get(i)-1);// Check if the amount of number i reached 0// meaning the number should be removed from the minHif(count.get(i)==0){// Current number should be identical to the minimum num// of the minH, to prevent missing middle number in the groupif(i != minH.peek())returnfalse;
minH.poll();}}}returntrue;}}