An interview to take you through the time complexity of the recursive algorithm

Hello everyone, I am the code king. I have been engaged in technical research and development at BAT for many years. I use my spare time to repaint leetcode. I hope to combine my years of practical experience to explain the algorithm more clearly. More original articles are welcome to pay attention to the public number "Code Random Notes" .

Time complexity of recursive algorithm

Many students do not know much about the time complexity of recursive algorithms

The same problem, also using the recursive algorithm, some students wrote O (n) code, and some students wrote O (logn) code

This is why it is caused by insufficient understanding of the time complexity of recursion

If the reader who is reading this article is also ignorant of the time complexity of recursion, I hope you can read it patiently and you will definitely gain something.

Here I want to take you through a simple interview question to gradually analyze the time complexity of the recursive algorithm and finally find the optimal solution.

Ask the interviewer

Take a look at this interview question: Find the nth power of x

Think about how to write such a simple question code.

The most intuitive way should be, a for loop to find the result, the code is as follows

int function1(int x, int n) {
    int result = 1;  // 注意 任何数的0次方等于1
    for (int i = 0; i < n; i++) {
        result = result * x;
    }
    return result;
}

Time complexity isO(n)

Interviewer second question

At this point the interviewer will say, is there an algorithm with better efficiency?

If the students have no ideas at this time, it is recommended not to say: I will not, I do not know. Can discuss with the interviewer, ask: can you give some tips.

The interviewer will generally prompt: consider the recursive algorithm

Some students have written a recursive algorithm as follows, using recursion to solve this problem

int function2(int x, int n) {
    if (n == 0) {
        return 1; // return 1 同样是因为0次方是等于1的
    }
    return function2(x, n - 1) * x;
}

The interviewer asked: What is the time complexity of this code?

Some students may think of it as soon as they see recursion logn, but this is not the case

The time complexity of the recursive algorithm is essentially dependent on: the number of recursion * the number of operations in each recursion

Then let's look at the code again, we have recursed several times.

Each time n-1, the time complexity is recursed n times O(n), each time a multiplication operation is performed, the time complexity of the multiplication operation is a constant termO(1)

So the time complexity of this code is n * 1 = O(n)

This time complexity may not meet the interviewer's expectations.

Interviewer three questions

So the classmates wrote the code of such a recursive algorithm as follows, to find the nth power of x

int function3(int x, int n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return function3(x, n/2) * function3(x, n/2)*x;
    }
    return function3(x, n/2) * function3(x, n/2);
}

When the interviewer saw it, he smiled slightly and asked how much time complexity of the code?

Let's analyze

First look at how many times the recursion, you can abstract the number of recursion into a full binary tree.

The algorithm we just wrote can be represented by a full binary tree (I chose n to be an even number for convenience), as shown in the figure:

Insert picture description here

At present, this binary tree is the case where x is raised to the nth power, and n is 16.

How many times do we perform multiplication when n is 16

Each node in this tree represents a recursion and a multiplication operation

So how many times the recursion is done depends on how many nodes there are in this tree.

Students who are familiar with the binary tree should know how to find the number of nodes in the binary tree

The number of nodes in this full binary tree is2^3 + 2^2 + 2^1 + 2^0 = 15

Some students will find that this is actually the sum formula of the proportional sequence. If you do not understand, you can write down this conclusion directly.

This conclusion often appears in interview questions related to binary trees.

So if it is to find the nth power of x, how many nodes does this recursive tree have, as shown in the figure below

Insert picture description here

-1After ignoring the constant term in time complexity , we find that the time complexity of this recursive algorithm is still O (n).

At this time, the interviewer will ask, it seems that the recursive algorithm is still the same O(n), obviously it does not meet the interviewer's expectations

Interviewer four questions

Then think about the O(logn)recursive algorithm should be how to write

Here is a reminder of the code of the recursive algorithm just given above, is there any redundancy?

Look at this optimized recursive algorithm code

int function4(int x, int n) {
    if (n == 0) {
        return 1;
    }
    int t = function4(x, n/2);// 这里相对于function3,是把这个递归操作抽取出来
    if (n % 2 == 1) {
        return t*t*x;
    }
    return t*t;
}

Let's take a look at the time complexity

Still see how many times he recursed

We can see that there is only one recursive call, and each time is n / 2

So here we call log with log 2 as low as n times

Each recursion is a multiplication operation, which is also a constant operation,

So the time complexity of this recursive algorithm is the real O (logn).

If the students finally write such code and the time complexity analysis is very clear, I believe the interviewer is more satisfied.

to sum up

Finally, I hope that through such a simple interview question, everyone will really understand how to analyze the time complexity of the recursive algorithm.
Insert picture description here

Published 236 original articles · praised 251 · 270,000 views +

Guess you like

Origin blog.csdn.net/youngyangyang04/article/details/105480308