Maximum Disjoint Triples Summing to 15
Dynamic ProgrammingAuto-graded · 26 test casesLast updated
Find the most disjoint three-card groups that each sum to 15, using memoized search over value counts because greedy choices can block better groupings.
Maximum Disjoint Triples Summing to 15
You have a collection of cards, each showing an integer value. A move picks any three cards still in the collection whose values add up to exactly 15, removes them for good, and earns one point. You may repeat moves in any order until you choose to stop or no valid triple remains.
Return the largest total number of points that can be earned.
Each card can be used at most once. Cards with the same value are interchangeable, so two cards showing 5 are different physical cards but equivalent for scoring. A triple may contain repeated values, for example (5, 5, 5) or (1, 1, 13), as long as enough cards with that value are available.
Function
Implement max_fifteen_triples(cards), where cards is a list of integers. It returns an integer: the maximum number of disjoint triples of cards, each summing to 15.
Notes
- A list with fewer than 3 cards scores 0.
- Choosing a triple greedily can prevent a better overall result, so make sure your approach considers alternatives.
Constraints
0 <= len(cards) <= 501 <= cards[i] <= 14- The solution should run in about one second.
Examples
Example 1
Input: max_fifteen_triples([5, 5, 5, 10, 10, 10])
Output: 1
Example 2
Input: max_fifteen_triples([1, 2, 3, 4, 14, 13])
Output: 0
Hints
Hint 1
Cards with the same value are interchangeable, so you don't need to track individual cards. Describe the situation by how many cards of each value remain.
Hint 2
Greedy can fail. For [4, 4, 5, 5, 5, 6, 6], taking (5, 5, 5) first gives 1 point, but (4, 5, 6) twice gives 2. Try choices and memoize the results by remaining counts.
Hint 3
To avoid exploring the same set of triples in many orders, always look at the smallest remaining value v. Either never use that card (discard it), or use it as the smallest member of a triple (v, b, c) with v <= b <= c and v + b + c = 15.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Greedy does not work. With [4, 4, 5, 5, 5, 6, 6], taking (5, 5, 5) first yields 1 point and leaves nothing, while (4, 5, 6) used twice yields 2. So we need to search over choices.
Compress the state. Equal values are interchangeable, so the state is a tuple of counts per value. A value of 14 can never be in a triple (the other two cards would need to sum to 1), so those cards are ignored. A 13 can still be used via (1, 1, 13).
Branch on the smallest remaining value v. In any optimal set of triples, one of two things holds for a copy of v:
- It is not used. We discard it and recurse.
- It is used. Since
vis the smallest value present, it is the minimum of its triple, so we only try triples(v, b, c)withv <= b <= candv + b + c = 15. We check that enough cards are available, including when values repeat, then remove the triple, add 1 point and recurse.
Fixing "smallest value first" means each set of chosen triples is explored in only one canonical order, rather than once per order of removal. Memoizing on the count tuple merges states reached by different paths.
Why it is correct
Take any state and an optimal collection of disjoint triples for it. Let v be the smallest value present. If some triple uses a copy of v, then v is that triple's minimum, and our enumeration tries exactly that triple type. The rest of the collection remains valid in the reduced state. If no triple uses v, removing one copy leaves the collection valid, which the discard branch covers. Every branch corresponds to a legal play, so nothing is overcounted. The order of moves doesn't affect the score, so viewing the answer as a set of disjoint triples is valid.
Complexity
- States: at most the product of (count + 1) over the 13 usable values. Far fewer are reachable in practice, because only the 19 triple shapes and single discards are ever subtracted, and values below the current minimum are always zero. This is comfortable for n <= 50.
- Time per state: O(1), with at most 7 triple options plus one discard.
- Space: O(number of states) for the memo, with recursion depth at most about 50, since each call removes at least one card.
Reference solution
from functools import lru_cache
from typing import List
MAX_V = 13 # a 14 would need the other two cards to sum to 1, which is impossible
def max_fifteen_triples(cards: List[int]) -> int:
counts = [0] * (MAX_V + 1)
for c in cards:
if 1 <= c <= MAX_V:
counts[c] += 1
# triples grouped by their smallest value
by_min = {v: [] for v in range(1, MAX_V + 1)}
for a in range(1, MAX_V + 1):
for b in range(a, MAX_V + 1):
c = 15 - a - b
if b <= c <= MAX_V:
by_min[a].append((a, b, c))
@lru_cache(maxsize=None)
def solve(state):
if sum(state) < 3:
return 0
v = 0
for i in range(1, MAX_V + 1):
if state[i] > 0:
v = i
break
if v == 0:
return 0
s = list(state)
# Option 1: this copy of v is never used.
s[v] -= 1
best = solve(tuple(s))
s[v] += 1
# Option 2: v is the smallest member of some triple.
for a, b, c in by_min[v]:
s[a] -= 1
s[b] -= 1
s[c] -= 1
if s[a] >= 0 and s[b] >= 0 and s[c] >= 0:
best = max(best, 1 + solve(tuple(s)))
s[a] += 1
s[b] += 1
s[c] += 1
return best
return solve(tuple(counts))