Hand of Straights LeetCode solution in Java

1
class Solution {
2
public boolean isNStraightHand(int[] hand, int groupSize) {
3
if (hand.length % groupSize != 0) return false;
4

5
Map<Integer, Integer> map = new HashMap<>();
6
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
7

8
for (int card : hand) {
9
if (map.containsKey(card)) map.put(card, map.get(card) + 1);
10
else {
11
map.put(card, 1);
12
minHeap.add(card);
13
}
14
}
15

16
while (!minHeap.isEmpty()) {
17
int min = minHeap.peek();
18
for (int i = min; i < min + groupSize; i++) {
19
if (!map.containsKey(i) || map.get(i) == 0) return false;
20
map.put(i, map.get(i) - 1);
21
if (map.get(i) == 0) {
22
if (minHeap.peek() != i) return false;
23
minHeap.poll();
24
}
25
}
26
}
27
return true;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0