-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoPlan0602
47 lines (46 loc) · 1.42 KB
/
AlgoPlan0602
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
# X of a deck of cards
##key idea:
- find common denominator of all counts of different numbers.
- fcd: if int !=0 find its modulus against the other int, and find the cd of the modulus against the other int again (recursive)
- compare current value with the next value, store its common denominator. as long as the cd is >= 2, should be fine.
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
Map<Integer,Integer> map = new HashMap();
for (int k = 0; k<deck.length;k++){
if (map.containsKey(deck[k])){
map.put(deck[k], map.get(deck[k])+1);
}else{
map.put(deck[k],1);
}
}
int x= 0;
int pre = 0;
int temp = 0;
for (int value: map.values()){
System.out.print(" This value is "+ value);
if (value <2){
return false;
}else{
if (pre ==0){
pre = value;
}else{
temp = findcd(pre,value);
if (temp <2){
return false;
}else{
x = temp;
}
pre = value;
}
}
}
return true;
}
//all larger than 2
public int findcd(int a, int b){
if (a ==0){
return b;
}
return findcd(b% a, a);
}
}