当前位置:网站首页>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)
边栏推荐
- 【二叉树】删点成林
- 数据库安全的重要性
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- What if does not match your user account appears when submitting the code?
- [爬虫]使用selenium时,躲避脚本检测
- JS to convert array to tree data
- idm服务器响应显示您没有权限下载解决教程
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
猜你喜欢

Simple network configuration for equipment management

"Series after reading" my God! It's so simple to understand throttling and anti shake~

H3C HCl MPLS layer 2 dedicated line experiment

2022危险化学品生产单位安全生产管理人员考题及在线模拟考试

EPP+DIS学习之路(1)——Hello world!

leetcode刷题:二叉树23(二叉搜索树中的众数)

Minimalist movie website

金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)

NPM instal reports agent or network problems

What if does not match your user account appears when submitting the code?
随机推荐
Preorder, inorder and postorder traversal of binary tree
Experiment with a web server that configures its own content
Error in compiling libssl
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
SQL Lab (46~53) (continuous update later) order by injection
VSCode的学习使用
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
【从 0 开始学微服务】【03】初探微服务架构
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
What if does not match your user account appears when submitting the code?
Processing strategy of message queue message loss and repeated message sending
sql-lab (54-65)
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
Epp+dis learning path (1) -- Hello world!
平安证券手机行开户安全吗?
利用棧來實現二進制轉化為十進制
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Configure an encrypted web server
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
【统计学习方法】学习笔记——支持向量机(上)