Count Reachable Totals from a Bounded Coin Collection
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Count the distinct positive totals obtainable from a limited supply of coins per denomination, testing bounded subset-sum reachability with DP or bitset techniques.
Reachable Totals with Limited Coins
A wallet holds several kinds of coins. You are given two lists of equal length, values and quantities. For each index i, the wallet contains exactly quantities[i] coins, each worth values[i]. Coins of the same kind are indistinguishable, and a coin can be used at most once.
Choose any selection of coins from the wallet (using between 0 and quantities[i] coins of kind i). The total worth of the selection is its sum.
Return the number of distinct positive totals that can be produced this way.
Details:
- A total counts once, no matter how many different selections produce it.
- The total
0(selecting nothing) is not counted. - Two entries may share the same value; they are treated as separate kinds, each with its own quantity.
- An entry with quantity
0contributes nothing. - If no positive total is possible (for example, empty lists or all quantities zero), return
0.
Your solution should remain efficient when quantities are large and the maximum possible total (the sum of values[i] * quantities[i]) is on the order of 10^6. Be ready to explain the time and space complexity of your approach.
Constraints
valuesandquantitieshave the same lengthn, withn >= 0.values[i] > 0for everyi.quantities[i] >= 0for everyi.- The maximum possible total
sum(values[i] * quantities[i])is at most about10^6. - The function returns a single integer.
Examples
Example 1
Input: count_reachable_totals([1, 2, 5], [2, 2, 1])
Output: 11
Example 2
Input: count_reachable_totals([2, 3, 7], [1, 1, 1])
Output: 7
Example 3
Input: count_reachable_totals([1, 1, 3], [1, 1, 1])
Output: 5
Example 4
Input: count_reachable_totals([5, 10], [0, 2])
Output: 2
Hints
Hint 1
You only need to know which totals are reachable, not how many ways produce them, so a boolean table indexed by total is enough.
Hint 2
Treating each coin as its own 0/1 item is too slow for big quantities. Split a quantity c into chunks 1, 2, 4, ... and a remainder; any number from 0 to c is a sum of a subset of those chunks.
Hint 3
Process each chunk as a 0/1 item, updating totals from high to low (or use a big-integer bitset with `mask |= mask << weight`) so a chunk is never applied twice. Finally exclude total 0 when counting.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a bounded subset-sum reachability problem. We only need to know which totals can be formed, so a boolean table is sufficient.
Let S = sum(values[i] * quantities[i]), the largest total possible. Keep a table reachable[0..S] where reachable[t] is true if total t can be formed. Initially only reachable[0] is true (the empty selection).
Treating every coin as a separate 0/1 item would cost roughly O(S * sum(quantities)), too slow for large quantities. Instead, use binary splitting: a quantity q is split into chunks 1, 2, 4, ..., 2^(m-1) and a remainder r, where the remainder is whatever is left. Each chunk becomes one 0/1 item with weight value * chunk_size.
Each chunk is applied with the standard 0/1 update, scanning totals from high to low:
for t from high down to weight:
if reachable[t - weight]: reachable[t] = true
Scanning downward guarantees that reachable[t - weight] has not yet been changed by the current chunk, so each chunk is used at most once.
The upper end of the scan is bounded by the total weight of the chunks processed so far, which avoids useless work without changing the result.
The answer is the number of true entries in reachable[1..S], excluding total 0.
Why it is correct
- Subsets of the chunks
1, 2, ..., 2^(m-1)produce every integer in[0, 2^m - 1]. The remainderrsatisfiesr <= 2^m, so adding it covers[r, q]. These two ranges overlap or touch, so every count in[0, q]is achievable. - All chunks sum to exactly
q, so no selection can use more thanqcoins. - Hence choosing a subset of chunks is equivalent to choosing any number of coins from
0toq, and reachability is preserved exactly. - The boolean table automatically deduplicates totals reachable in several ways, and skipping index 0 excludes the empty selection.
Complexity
- Time:
O(S * sum(log2(quantities[i] + 1))), since each chunk costs at mostO(S). - Space:
O(S)for the byte table.
For large S, a Python big-integer bitset (mask |= mask << weight) performs each chunk update in C and is much faster in practice, with the same asymptotic structure divided by the word size.
Reference solution
from typing import List
def count_reachable_totals(values: List[int], quantities: List[int]) -> int:
total = sum(v * q for v, q in zip(values, quantities))
reachable = bytearray(total + 1)
reachable[0] = 1
processed = 0
for v, q in zip(values, quantities):
if q <= 0:
continue
# Binary splitting turns a bounded quantity into O(log q) 0/1 items.
k = 1
remaining = q
while remaining > 0:
take = min(k, remaining)
weight = v * take
for s in range(processed + weight, weight - 1, -1):
if reachable[s - weight]:
reachable[s] = 1
processed += weight
remaining -= take
k *= 2
return sum(reachable[1:])