-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c857eb
commit ffada26
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
914. X of a Kind in a Deck of Cards | ||
Solved | ||
Easy | ||
Topics | ||
Companies | ||
You are given an integer array deck where deck[i] represents the number written on the ith card. | ||
Partition the cards into one or more groups such that: | ||
Each group has exactly x cards where x > 1, and | ||
All the cards in one group have the same integer written on them. | ||
Return true if such partition is possible, or false otherwise. | ||
*/ | ||
class Solution { | ||
public boolean hasGroupsSizeX(int[] deck) { | ||
if(deck.length<2) return false; | ||
Map<Integer,Integer> a = new HashMap<>(); | ||
for(int i=0;i<deck.length;i++){ | ||
a.put(deck[i],a.getOrDefault(deck[i],0)+1); | ||
} | ||
|
||
int ans = 0; | ||
|
||
for(int key: a.keySet()){ | ||
|
||
ans = gcd(ans, a.get(key)); | ||
} | ||
|
||
return ans>=2 ? true : false; | ||
|
||
|
||
} | ||
|
||
public int gcd(int a, int b){ | ||
if(b==0){ | ||
return a ; | ||
} | ||
return gcd(b, a%b); | ||
} | ||
} |