Data Structures & AlgorithmsHardICPC regional · counting DP

Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code.

Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code.
t = [1, 1, 2]
for n in range(3, 11):
    t.append(t[n - 1] + t[n - 2] + t[n - 3])
print(t[10])
What is printed?
  1. A. 274
  2. B. 149
  3. C. 81
  4. D. 504

Correct answer: A. 274

The recurrence t(n) = t(n-1) + t(n-2) + t(n-3) counts the last part. Counting unordered partitions instead gives a much smaller number, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic