Skip to content

Commit 1d2dd1e

Browse files
committed
914. X of a Kind in a Deck of Cards
1 parent 537d0f1 commit 1d2dd1e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
1. 아이디어: 크기가 1보다 큰 파티션으로 배열 나누기
3+
이때 각 파티션은 같은 숫자로만 이루어져야 한다.
4+
5+
최대공약수 - 유클리드 호제법 사용.
6+
먼저 숫자의 빈도수를 세고, 첫 빈도수 기준 최대 공약수를 각각 계산한다.
7+
8+
2. 시간복잡도: O(10000*logN)
9+
10+
3. 자료구조/알고리즘: 최대공약수, 카운팅
11+
12+
*/
13+
14+
class Solution {
15+
public boolean hasGroupsSizeX(int[] deck) {
16+
int[] cnt = new int[10001];
17+
18+
for(int i=0; i<deck.length; i++) cnt[deck[i]]++;
19+
20+
int g = -1;
21+
for(int i=0; i<10001; i++) {
22+
if(cnt[i] == 0) continue;
23+
24+
if(g == -1) g = cnt[i];
25+
else g = gcd(g, cnt[i]);
26+
}
27+
return g > 1;
28+
29+
}
30+
31+
private int gcd(int a, int b) {
32+
if(b==0) return a;
33+
return gcd(b, a%b);
34+
}
35+
}

0 commit comments

Comments
 (0)