[Leetcode-914-x of a Kind in a Deck of Cards]

Source: Internet
Author: User

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. 1 <= deck.length <= 10000
      2. 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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.