当前位置:网站首页>Master formula. (used to calculate the time complexity of recursion.)
Master formula. (used to calculate the time complexity of recursion.)
2022-07-07 12:45:00 【Kinght_ one hundred and twenty-three】
One 、Mster The formula
Prerequisite :
- The size of the recursive subproblem must be the same .
Now let me explain , The specific meaning of this formula .
First a Is the number of calls on the scale of a subproblem of a recursive problem ,d Is the number of subproblems , hinder N Of d The power is the call except for the recursive subproblem , The time complexity of the scale of other problems .
for instance :
def func(arr, l, r): # Again arr The largest value found in the array
if l == r:
return arr[l]
mid = l + ((r - l) >> 1)
l_max = func(arr, l, mid)
r_max = func(arr, mid + 1, r)
return max(l_max, r_max)
For example, the above code is a simple recursion .
Well, its T(N) = 2 * T(N / 2) + O(1)
Because every time you call the main function , You need to call the function of the subproblem with the same scale twice , So for 2 * T(N / 2), And because it calls functions of subproblems , Other time complexity is O(1). So this is the recursive Master The formula .
Two 、 Time complexity of calculation
According to this formula and our code above , The time complexity should be O(N)
边栏推荐
猜你喜欢
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
30. Feed shot named entity recognition with self describing networks reading notes
图像像素读写操作
Epp+dis learning road (2) -- blink! twinkle!
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
[pytorch practice] use pytorch to realize image style migration based on neural network
File upload vulnerability - upload labs (1~2)
On valuation model (II): PE index II - PE band
What is an esp/msr partition and how to create an esp/msr partition
随机推荐
Minimalist movie website
File upload vulnerability - upload labs (1~2)
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
Is it safe to open an account in Ping An Securities mobile bank?
【深度学习】图像多标签分类任务,百度PaddleClas
30. Feed shot named entity recognition with self describing networks reading notes
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Typescript interface inheritance
Object. Simple implementation of assign()
Connect to blog method, overload, recursion
BGP third experiment report
Day-20 file operation, recursive copy, serialization
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
Charles: four ways to modify the input parameters or return results of the interface
Day-14 common APIs
Static routing assignment of network reachable and telent connections
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
2022聚合工艺考试题模拟考试题库及在线模拟考试
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
Simple implementation of call, bind and apply