Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

What is printed? (unique paths count with memoised recursion vs closed form)

What is printed? (unique paths count with memoised recursion vs closed form)
from functools import lru_cache
from math import comb
@lru_cache(None)
def up(r, c):
    if r == 1 or c == 1: return 1
    return up(r - 1, c) + up(r, c - 1)
print(up(3, 7), comb(8, 2), up(10, 10))
  1. A. 21 28 48620
  2. B. 28 28 184756
  3. C. 28 28 48620
  4. D. 28 36 48620

Correct answer: C. 28 28 48620

An r × c grid needs (r − 1) downs and (c − 1) rights in some order: C(r + c − 2, r − 1). 3 × 7 → C(8, 2) = 28; 10 × 10 → C(18, 9) = 48620. Memoised recursion is O(r·c); without memoisation it is exponential.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic