[Inscription] card group -lootcode

Title: Card Packet

Given a deck of cards, each card are written on an integer.

At this point, you need to select a digital X, so that we can be full decks of cards according to the following rules into one or more groups:

Each group has X cards.
Within the group are written the same integer on all the cards.
> = 2 returns true only if you optional X.

 

Example 1:

Input: [1,2,3,4,4,3,2,1]
Output: true
interpretation: grouping is possible [1,1], [2,2], [3,3], [4,4 ]
example 2:

Input: [1,1,1,2,2,2,3,3]
Output: false
interpretation: the packet does not satisfy the requirements.
Example 3:

Input: [1]
Output: false
interpretation: the packet does not satisfy the requirements.
Example 4:

Input: [1,1]
Output: true
interpretation: grouping is possible [1,1]
Example 5:

Input: [1,1,2,2,2,2]
Output: true
interpretation: grouping is possible [1,1], [2,2], [2,2]

prompt:

1 <= deck.length <= 10000
0 <= deck[i] < 10000


 

Ideas:

  The first condition of the subject, into a card will be required or more groups, and each group is the same card.

  We can imagine that if only one card in each group, then no matter how many cards are all eligible sub-laws.

  If there are two cards in each group, then the total amount of each of the same brand must be a multiple of 2, have a points system in line with the conditions.

  If there are three cards in each group, then the total number of the same brand of each one must be a multiple of 3, only the points system in line with the conditions.

 

  By the same token, we are looking for is the number of x:

    ★: A collecting y

  1. The total number of cards are all the same multiple of y elements, that is, the elements are divisible by y in
  2.  y x is the number of elements as large as possible

  Obviously, y is the number of the set of cards all of the same number of conventions, x is their greatest common divisor.

 

The codes (c):

// greatest common divisor 
int
GCD ( int X, int Y) { int tmp; the while (! Y = 0 ) { tmp = x; x = y; y = tmp%y; } return x; } bool hasGroupsSizeX(int* deck, int deckSize){ int count[10005] = {0}; int x = -1; for( int i = 0; i < deckSize; i++ ) { count[deck[i]]++; } for( int i = 0; i < 10005; i++ ) { if( count[i] > 0 ) { if( x == -1 ) ans = count[i]; else x = gcd( ans, count[i] ); } } if( x >= 2 ) return true; return false; }

 

2020-03-27-19:03:11

 

Guess you like

Origin www.cnblogs.com/Sxccz/p/leetcode_10.html