class Solution {
public boolean isNStraightHand(int[] hand, int W) {
if (hand.length % W != 0) return false;
if (W == 1) return true;
List<Integer> hands = new ArrayList();
for (int val : hand) hands.add(val);
Collections.sort(hands);
return helper(hands, W);
}
private boolean helper(List<Integer> hands, int W) {
// System.out.println(hands);
int n = hands.size();
if (n == 0) return true;
if (n % W != 0) return false;
if (W == 1) return true;
int k = 1;
Iterator<Integer> iter = hands.iterator();
int smallest = iter.next();
iter.remove();
while (iter.hasNext() && k < W) {
if (iter.next() == smallest + k) {
k ++;
iter.remove();
}
}
if (k == W) {
return helper(hands, W);
}
return false;
}
}
Solution2:
class Solution {
public boolean isNStraightHand(int[] hand, int W) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for(int i : hand){
minHeap.add(i);
}
while(minHeap.size() != 0) {
int start = minHeap.poll();
for(int j = 1; j < W; j++){
if(minHeap.remove(start + j)) {
continue;
}
else {
return false;
}
}
}
return true;
}
}