PlayCards Problem in Google Code Jam China

Problem Statement

You are playing a card game, and in your hand, you are holding several cards. Each card has a suit, 'S', 'H', 'D', or 'C', and a value between 1 and 10, inclusive. You may play cards as part of a set, which is three or more cards of the same value, or as part of a run, which is three or more cards of the same suit, in sequential order. (Runs may not wrap, thus, 9-10-1 is not a valid run.) Each card may be played only once.
For example, "1 S", "1 H" and "1 D" would be a valid set. "2 S", "3 S", and "4 S" would be a valid run.
You want to play as many cards as possible, maybe in several plays (see example 4). Given a String[] cards representing the cards held in your hand, you are to return an int indicating the maximum number of cards you can play. Each card will be given in the form "value suit" (quotes added for clarity).

Definition
Class: PlayCards
Method: maxCards
Parameters: String[]
Returns: int
Method signature: int maxCards(String[] cards)
(be sure your method is public)

Constraints
-
cards will contain between 0 and 20 elements, inclusive.
-
No two elements of cards will be the same.
-
Each element of cards will be of the form "value suit" (quotes added for clarity).
-
Each number represented will be between 1 and 10, inclusive, with no leading zeroes.
-
Each suit represented will be 'S', 'H', 'D', or 'C'.

Examples
0){"1 S", "2 S", "3 S"}
Returns: 3
We have a run of three cards, which we can play.
1){"4 C", "4 D", "4 S", "3 S", "2 S"}
Returns: 3
We can take the 4's as a set, or we can take the 2-3-4 run. Either way, we play 3 cards.
2) {"1 S", "2 S", "2 H", "3 H", "3 D", "4 D", "4 C", "5 C", "5 S"}
Returns: 0
We've got lots of cards, but no way to put three together.
3){"1 S", "2 S"}
Returns: 0
Since we have to play at least three cards at a time, there's nothing to do here.
4){"1 S", "2 S", "10 S", "5 S", "8 S",
"3 H", "9 H", "6 H", "5 H", "4 H",
"10 D", "5 D", "7 D", "4 D", "1 D",
"2 C", "4 C", "5 C", "6 C", "7 C"}
Returns: 9
The best we can do is to take the set of 4s, the 5-6-7 C, and the remaining three 5s. We could have taken the 4-5-6-7 of C, or all four 5s, but we would not end up playing as many cards.

(c)2003, TopCoder, Inc. All rights reserved.

 

The point is to get the best way to play cards. I thought this way to resolve this problem.

1.Store all possibility ways to play cards.

For Example:{"4 C", "4 D", "4 S", "3 S", "2 S","4 H","5 S","6 S","7 S"}

0 4C 4D 4H 4S
1 4D 4H 4S
2 4C 4H 4S
3 4C 4D 4S
4 4C 4D 4H
5 2S 3S 4S 5S 6S 7S
6 2S 3S 4S
7 3S 4S 5S
8 4S 5S 6S
9 5S 6S 7S
10 2S 3S 4S 5S
11 3S 4S 5S 6S
12 4S 5S 6S 7S
13 2S 3S 4S 5S 6S
14 3S 4S 5S 6S 7S

2.Circle the arraylist. When a element going out, we check the left elements whose id is bigger than the gone element in the array to see if they was destroyed.

For example:
0 go out, all the elements are destroyed. Game over.
Back to 0, let 1 go and 2 3 4 ...

Then do another circle in the left arraylist. When there is a element going out, we record the length of this element and add this value to the value we record in previous circle.

3.Print the max value we recorded. This is the max cards we can play.

4.The code:

posted @ Thursday, December 29, 2005 3:56 PM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
Please add 2 and 6 and type the answer here:
 

Live Comment Preview:

 
«January»
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567