当前位置:网站首页>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)
边栏推荐
猜你喜欢
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Attack and defense world ----- summary of web knowledge points
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
opencv的四个函数
BGP actual network configuration
leetcode刷题:二叉树23(二叉搜索树中的众数)
【统计学习方法】学习笔记——第五章:决策树
Several ways to clear floating
Tutorial on the principle and application of database system (011) -- relational database
AirServer自动接收多画面投屏或者跨设备投屏
随机推荐
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Experiment with a web server that configures its own content
Customize the web service configuration file
DOM parsing XML error: content is not allowed in Prolog
【统计学习方法】学习笔记——提升方法
[爬虫]使用selenium时,躲避脚本检测
【从 0 开始学微服务】【01】什么是微服务
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
Tutorial on principles and applications of database system (009) -- conceptual model and data model
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
浅谈估值模型 (二): PE指标II——PE Band
Utiliser la pile pour convertir le binaire en décimal
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
Static routing assignment of network reachable and telent connections
On valuation model (II): PE index II - PE band
Typescript interface inheritance
Polymorphism, final, etc
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
通讯协议设计与实现