-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path914.java
More file actions
56 lines (55 loc) · 1.46 KB
/
914.java
File metadata and controls
56 lines (55 loc) · 1.46 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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
if(deck==null || deck.length<2) return false;
int[]count=new int[1000];
for(int i:deck){
count[i]++;
}
int g=-1;
for(int m: count) {
if(m>0) g=g==-1? m: gcd(g, m);
}
return g>=2;
}
public int gcd(int x, int y) {
return x==0?y:gcd(y%x, x);
}
}
__________________________________________________________________________________________________
sample 37152 kb submission
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i: deck){
hm.putIfAbsent(i,0);
hm.compute(i,(x,y)->y+1);
}
int size=Integer.MAX_VALUE;
for(int i: hm.values()){
if(i<size){
size=i;
}
}
if(size<2){
return false;
}
for(int i: hm.values()){
if(hcf(size,i)==1){
return false;
}
}
return true;
}
int hcf(int a, int b){
int hcf=1;
for(int i=1;i<=a && i<=b;i++){
if(a%i==0 &&b%i==0){
hcf=i;
}
}
return hcf;
}
}
__________________________________________________________________________________________________