当前位置:网站首页>Time complexity & space complexity
Time complexity & space complexity
2022-07-07 04:52:00 【ᝰꫛꪮꪮꫜ*】
Ha ha ha ha , Let's talk nonsense first , Today I know how to add emoticons to the content , Um. , So I gave it a try , So happy 🥰🤪🧨
【 Preface 】
- Time complexity mainly measures the running speed of an algorithm
- Spatial complexity mainly measures the additional space required by an algorithm
In the early days of computer development , The storage capacity of the computer is very small . So I care about space complexity . But after the rapid development of the computer industry , The storage capacity of the computer has reached a high level . So now we don't need to pay special attention to the spatial complexity of an algorithm .
Catalog
Time complexity
1. The number of times the basic operations in the algorithm are executed , Time complexity of the algorithm
2. We use it Big O Progressive method Time complexity ( We can understand it as the approximate number of executions )
- With constant 1 Replace all the addition constants in runtime .
- In the modified run times function , Only the highest order terms .
- If the highest order term exists and is not 1, The constant multiplied by this item is removed
- The result is big O rank
️ For example, the following code block :

Two for Loop plus one while loop , Execution times are N^2 + 2* N + 10
Big use O Progressive method , Keep only the highest order and use 1 Instead of constants , Therefore, the time complexity of this function is O(N^2)
- Usually, the time complexity of recursive algorithm is 0(N)
( But what? , Recursion of Fibonacci sequence , The time complexity is O(2^N))
- Every time *n perhaps /n operation , The general time complexity is O(log n), For example, binary search is
( The fourth question of Li Kou just done today , It clearly limits the time complexity of the algorithm to
O(log (m+n)), So sad , I didn't expect to use two points , Specially, I'll sort out the time complexity Topic link :Find the median of two positive arrays - Power button (LeetCode) (leetcode-cn.com))
Spatial complexity
1. Space complexity is a measure of the amount of storage space temporarily occupied by an algorithm during operation
!!! Yes. Additional development Array space size of , It's not how much the program takes bytes Space of oh
2. The space complexity is also large O Progressive method , No more details
Usually , The space complexity of recursion is also O(n)
️️️ Problem practice
Time complexity :
1.
int binarySearch(int[] array, int value) { int begin = 0; int end = array.length - 1; while (begin <= end) { int mid = begin + ((end-begin) / 2); if (array[mid] < value) begin = mid + 1; else if (array[mid] > value) end = mid - 1; else return mid; } return -1;}【 answer 】O(logN) Two points search
2.
long factorial(int N) {return N < 2 ? N : factorial(N-1) * N;}【 answer 】 Time complexity The space complexity is O(N) recursive N Time
3.
int fibonacci(int N) {return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2);}【 answer 】O(2 ^ N)
边栏推荐
猜你喜欢

acwing 843. N-queen problem

What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key

Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
![A detailed explanation of head pose estimation [collect good articles]](/img/22/7ae0b12c3d945b449bcc8bb4a8961b.jpg)
A detailed explanation of head pose estimation [collect good articles]

为什么很多人对技术债务产生误解

AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘

mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi

Vscode automatically adds a semicolon and jumps to the next line

JDBC link Oracle reference code

【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
随机推荐
jvm是什么?jvm调优有哪些目的?
JDBC link Oracle reference code
图灵诞辰110周年,智能机器预言成真了吗?
Kivy tutorial of setting the size and background of the form (tutorial includes source code)
Camera calibration (I): robot hand eye calibration
Jetson nano configures pytorch deep learning environment / / to be improved
Vscode 如何使用内置浏览器?
Data security -- 12 -- Analysis of privacy protection
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
一度辍学的数学差生,获得今年菲尔兹奖
acwing 843. n-皇后问题
A detailed explanation of head pose estimation [collect good articles]
计数排序基础思路
Master the secrets of software security testing methods, and pinch the security test report with your hands
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
What is JVM? What are the purposes of JVM tuning?
窗口可不是什么便宜的东西
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
Is there any way to bookmark the code in the visual studio project- Is there a way to bookmark code in a Visual Studio project?
leetcode 53. Maximum subarray maximum subarray sum (medium)