当前位置:网站首页>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)
边栏推荐
- Experiment with a web server that configures its own content
- EPP+DIS学习之路(2)——Blink!闪烁!
- [statistical learning methods] learning notes - improvement methods
- Epp+dis learning road (2) -- blink! twinkle!
- SQL Lab (46~53) (continuous update later) order by injection
- Processing strategy of message queue message loss and repeated message sending
- 【从 0 开始学微服务】【02】从单体应用走向服务化
- Minimalist movie website
- Vxlan static centralized gateway
- SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
猜你喜欢
Tutorial on principles and applications of database system (009) -- conceptual model and data model
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
opencv的四个函数
Aike AI frontier promotion (7.7)
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
30. Feed shot named entity recognition with self describing networks reading notes
[pytorch practice] write poetry with RNN
Connect to blog method, overload, recursion
How to use PS link layer and shortcut keys, and how to do PS layer link
随机推荐
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
数据库系统原理与应用教程(009)—— 概念模型与数据模型
leetcode刷题:二叉树21(验证二叉搜索树)
Connect to blog method, overload, recursion
Vxlan 静态集中网关
(待会删)yyds,付费搞来的学术资源,请低调使用!
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Vxlan static centralized gateway
平安证券手机行开户安全吗?
【从 0 开始学微服务】【00】课程概述
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
[statistical learning methods] learning notes - Chapter 5: Decision Tree
[疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
What is an esp/msr partition and how to create an esp/msr partition
opencv的四个函数
[pytorch practice] use pytorch to realize image style migration based on neural network