当前位置:网站首页>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)
- Session
- Polymorphism, final, etc
- About web content security policy directive some test cases specified through meta elements
- Charles: four ways to modify the input parameters or return results of the interface
- The left-hand side of an assignment expression may not be an optional property access. ts(2779)
- What is an esp/msr partition and how to create an esp/msr partition
- OSPF exercise Report
- 【统计学习方法】学习笔记——第五章:决策树
- 【统计学习方法】学习笔记——第四章:朴素贝叶斯法
猜你喜欢
leetcode刷题:二叉树20(二叉搜索树中的搜索)
The road to success in R & D efficiency of 1000 person Internet companies
Airserver automatically receives multi screen projection or cross device projection
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
[statistical learning method] learning notes - support vector machine (I)
SQL Lab (41~45) (continuous update later)
AirServer自动接收多画面投屏或者跨设备投屏
Routing strategy of multi-point republication [Huawei]
随机推荐
解密GD32 MCU产品家族,开发板该怎么选?
Polymorphism, final, etc
SQL Lab (41~45) (continuous update later)
Simple network configuration for equipment management
On valuation model (II): PE index II - PE band
DOM parsing XML error: content is not allowed in Prolog
[pytorch practice] write poetry with RNN
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
Configure an encrypted web server
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
[statistical learning methods] learning notes - Chapter 5: Decision Tree
Airserver automatically receives multi screen projection or cross device projection
2022聚合工艺考试题模拟考试题库及在线模拟考试
(待会删)yyds,付费搞来的学术资源,请低调使用!
【从 0 开始学微服务】【01】什么是微服务
Apache installation problem: configure: error: APR not found Please read the documentation
Is it safe to open an account in Ping An Securities mobile bank?
【PyTorch实战】用RNN写诗