Rummy Score
InterviewTime limit1sMemory limit1024 MB
Given seven cards per hand, find the minimum sum of card values left outside groups of three or more cards with equal or consecutive values.
- Level
Medium4 of 10
- Topics
- Backtracking, Brute force
- Solved
- No attempts yet
Problem
Mack and Zack, the twins of Dr. Orooji, play Rummy with a modified deck. Card values run from 1 to 13. The card 1 is only 1, not both 1 and 14. Cards have no suits. A deck may contain any number of cards of each value, unlike a standard deck that has exactly four of each.
You are given the seven cards in a Rummy hand held by Mack or Zack. Find the points lost by the hand.
In Rummy, a group is three or more cards with the same value or with consecutive values. Examples are {4, 4, 4}, {6, 6, 6, 6, 6}, {9, 10, 11}, and {1, 2, 3, 4}. The points lost by a hand is the sum of the values of the cards that belong to no group. Group the cards so that this sum is as small as possible. A card can be used in only one group. For example, in the hand {2, 2, 2, 3, 4, 8, 10}, the grouping {2, 3, 4} is better than {2, 2, 2}. The hand cannot form both {2, 2, 2} and {2, 3, 4}, because it has only three cards of value 2.
Input
The first line contains a positive integer n, the number of hands. Each of the next n lines contains seven integers from 1 to 13, separated by single spaces.
Output
For each hand, print "Rummy Hand: v", where v is the input values exactly as given. On the next line, print the points lost by the hand. Print a blank line after each test case.