-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path846.java
More file actions
64 lines (55 loc) · 2.27 KB
/
846.java
File metadata and controls
64 lines (55 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public boolean isNStraightHand(int[] hand, int W) {
int [] groups =new int[W];
for(int i=0;i<hand.length;i++) {
groups[hand[i]%W]++;
}
int val = groups[0];
for(int i=1;i<groups.length;i++) {
if (groups[i] != val) return false;
}
return true;
}
}
__________________________________________________________________________________________________
sample 38356 kb submission
import java.util.*;
class Solution {
public boolean isNStraightHand(int[] hand, int W) {
if (hand == null || hand.length % W != 0) {
return false;
}
int numberOfGroups = hand.length / W;
TreeMap<Integer, Integer> cardToCount = new TreeMap<>();
for (int i = 0; i < hand.length; i++) {
int card = hand[i];
int existingCards = cardToCount.getOrDefault(card, 0);
cardToCount.put(card, existingCards + 1);
}
for (int i = 0; i < numberOfGroups; i++) {
Integer lastCard = null;
for (int j = 0; j < W; j++) {
Map.Entry<Integer,Integer> leastKeyEntry = null; // more appropriate naming should be next larger key
if (j == 0) {
leastKeyEntry = cardToCount.firstEntry();
lastCard = leastKeyEntry.getKey();
} else {
leastKeyEntry = cardToCount.higherEntry(lastCard);
if (leastKeyEntry == null || leastKeyEntry.getKey() - lastCard != 1) {
return false;
}
lastCard = leastKeyEntry.getKey();
}
if (leastKeyEntry.getValue() - 1 == 0) {
cardToCount.remove(leastKeyEntry.getKey());
} else {
cardToCount.compute(leastKeyEntry.getKey(), (k, v) -> v - 1);
}
}
}
return true;
}
}
__________________________________________________________________________________________________