2. Space Time complexity (Theory)
it is not the time taken by code to run on system, as diff system have diff speed.
it is rate at which time taken increases wrt input size.
Big O notation- computed for worst case scenario
O(cn+k)=O(cn)
ω is lower bound
θ is avg complexity
O is upper bound
1 sec => O(10^8) time complexity
optimal: 1 > logn > n > nlogn > (n^2) > n!
----------------------------------------------------------------------------------------------------------------------------
Space Complexity
it is (auxiliary space + input space) = (space to solve + space to store)
int f1(vector<int>v) -- pass by value -- space inefficient, as it creates a copy
int f1(const vector<int>&v) -- for read only operations without making a copy
int f1(vector<int>&v) -- for write operations in the original one
----------------------------------
int : (-10^9, +10^9)
long: (-10^12, +10^12)
long long: (-10^18, +10^18)
-------------------------------------------------------------------------------------------------------------------------
Standard TIME complexities-
ordered set/map- logN
unordered set/map- 1 or (N: worst case)
------------------
sorting( merge/ quick/.sort() )- NlogN
bubble/ selection/ insertion sort- (n^2)
-----------------
max/min- n
binary search- logN
all permutations- n!
stack/queue- O(1)
priority queue- logN
-----------------
----------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment