Data Structures & AlgorithmsHardICPC regional · counting DP

Problem: How many ordered sequences of jumps of sizes 2, 3 or 5 sum exactly to 12? Run the DP.

Problem: How many ordered sequences of jumps of sizes 2, 3 or 5 sum exactly to 12? Run the DP.
ways = [1] + [0] * 12
for v in range(1, 13):
    for s in (2, 3, 5):
        if v >= s:
            ways[v] += ways[v - s]
print(ways[12])
What is printed?
  1. A. 22
  2. B. 18
  3. C. 27
  4. D. 25

Correct answer: C. 27

The ordered compositions of 12 into parts 2, 3 and 5 satisfy ways[v] = ways[v-2] + ways[v-3] + ways[v-5], which gives 27. Unordered counting would give a smaller number.

More Coding Interview Problems questions

Exams that ask this topic