In a deck of cards, with each card have an integer written on it.
Return true
if and only if your can choose X >= 2
such that it's possible to split the entire deck into 1 or more groups O f cards, where:
- Each group has exactly
X
cards.
- All the cards with each group has the same integer.
Example 1:
Trueexplanation:possible partition [1,1],[2,2],[3,3],[4,4]
Example 2:
FalseExplanation:no possible partition.
Example 3:
FalseExplanation:no possible partition.
Example 4:
Trueexplanation:possible partition [+]
Example 5:
Trueexplanation:possible partition [1,1],[2,2],[2,2]
Note:
1 <= deck.length <= 10000
0 <= deck[i] < 10000
Ideas:
Direct violence, first of all, x must be divisible by the length of deck n. and x must be divisible by the number of elements in the group.
BOOLHasgroupssizex (vector<int>&deck) { intn =deck.size (); if(N <=1)return false; Map<int,int>MP; for(inti =0; I < n; i++) {mp[deck[i]]++;} for(intx =2; x <= N; X + +) { if(n% x! =0)Continue; for(Auto it =Mp.begin ();;) { if(it->second% x! =0) { Break;} It++; if(It = = Mp.end ()) {return true;} } } return false;}
[Leetcode-914-x of a Kind in a Deck of Cards]