当前位置:网站首页>Master公式。(用于计算递归的时间复杂度。)
Master公式。(用于计算递归的时间复杂度。)
2022-07-07 10:33:00 【Kinght_123】

一、Mster公式

前提条件:
- 递归的子问题的规模必须是相同的。
下面让我来解释一下,这个公式具体的含义。
首先a是一个递归问题的子问题的规模的调用次数,d是子问题的数目,后面的N的d次方就是除了递归子问题的调用,其他问题规模的时间复杂度。
举个例子:
def func(arr, l, r): # 再arr数组中找到最大的数值
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)
比如上面的代码就是一个简单的递归。
那么的它的T(N) = 2 * T(N / 2) + O(1)
因为每次调用主函数,需要调用两次规模相同的子问题的函数,所以为2 * T(N / 2),又因为它除了调用子问题的函数,其他的时间复杂度为O(1).所以这就是这个递归的Master公式。
二、计算时间复杂度

按照这个公式和我们上面的代码,时间复杂度应该为O(N)
边栏推荐
- Several methods of checking JS to judge empty objects
- [statistical learning methods] learning notes - Chapter 5: Decision Tree
- ES底层原理之倒排索引
- SQL head injection -- injection principle and essence
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- 通讯协议设计与实现
- leetcode刷题:二叉树20(二叉搜索树中的搜索)
- NGUI-UILabel
- Object. Simple implementation of assign()
- Simple implementation of call, bind and apply
猜你喜欢

ENSP MPLS layer 3 dedicated line

BGP third experiment report
![[pytorch practice] use pytorch to realize image style migration based on neural network](/img/20/8ed7113115709b6169be289b0c280a.png)
[pytorch practice] use pytorch to realize image style migration based on neural network
![Routing strategy of multi-point republication [Huawei]](/img/5c/2e3b739ce7199f0d2a4ddd7c3856fc.jpg)
Routing strategy of multi-point republication [Huawei]

ES底层原理之倒排索引

leetcode刷题:二叉树19(合并二叉树)
![[爬虫]使用selenium时,躲避脚本检测](/img/3a/85ea729be2aa76c3de4a822ca6939b.png)
[爬虫]使用selenium时,躲避脚本检测

Session

leetcode刷题:二叉树20(二叉搜索树中的搜索)

【深度学习】图像多标签分类任务,百度PaddleClas
随机推荐
Multi row and multi column flex layout
Polymorphism, final, etc
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
利用栈来实现二进制转化为十进制
Several ways to clear floating
免备案服务器会影响网站排名和权重吗?
Realize all, race, allsettled and any of the simple version of promise by yourself
广州市召开安全生产工作会议
Configure an encrypted web server
AirServer自动接收多画面投屏或者跨设备投屏
ENSP MPLS layer 3 dedicated line
leetcode刷题:二叉树23(二叉搜索树中的众数)
SQL head injection -- injection principle and essence
平安证券手机行开户安全吗?
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Cookie
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
编译 libssl 报错
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
[pytorch practice] image description -- let neural network read pictures and tell stories