当前位置:网站首页>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)
边栏推荐
- 数据库系统原理与应用教程(007)—— 数据库相关概念
- 【统计学习方法】学习笔记——支持向量机(上)
- leetcode刷题:二叉树23(二叉搜索树中的众数)
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- 解密GD32 MCU产品家族,开发板该怎么选?
- Static vxlan configuration
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- Apache installation problem: configure: error: APR not found Please read the documentation
- How much does it cost to develop a small program mall?
猜你喜欢

Learning and using vscode

数据库系统原理与应用教程(007)—— 数据库相关概念

对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景

【PyTorch实战】用RNN写诗

ENSP MPLS layer 3 dedicated line

About web content security policy directive some test cases specified through meta elements

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

爱可可AI前沿推介(7.7)

Static routing assignment of network reachable and telent connections

ES底层原理之倒排索引
随机推荐
Epp+dis learning road (2) -- blink! twinkle!
Decrypt gd32 MCU product family, how to choose the development board?
The IDM server response shows that you do not have permission to download the solution tutorial
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
DOM parsing XML error: content is not allowed in Prolog
Cookie
NGUI-UILabel
2022聚合工艺考试题模拟考试题库及在线模拟考试
SQL blind injection (WEB penetration)
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
Charles: four ways to modify the input parameters or return results of the interface
gcc 编译报错
30. Feed shot named entity recognition with self describing networks reading notes
(to be deleted later) yyds, paid academic resources, please keep a low profile!
数据库系统原理与应用教程(007)—— 数据库相关概念
BGP actual network configuration
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
Tutorial on the principle and application of database system (008) -- exercises on database related concepts