What is the time complexity of a function with two parameters?

What is the time complexity of the following method? Two options give me a lot of confusion. Thanks in advance.

public int count(int m, int n) {
    if(m == 1 || n == 1) return 1;
    return count(m-1, n) + count(m, n-1);
}
+4
source share
1 answer

It is in O(2^(n+m)).

This can be proved using induction, where the step of induction:

T(n,m) = T(n-1,m) + T(n, m-1) =(*) 2^(n+m-1) + 2^(n+m-1) = 2*2^(n+m-1) = 2^(n+m)

Where (*) is the induction hypothesis.

+3
source

Source: https://habr.com/ru/post/1648285/


All Articles