Big "OH"
Before we proceed we must know what the below graph means
If you are wondering what it means let me be very straight forward to you.
"You have to write your algorithm with time and space complexity near or below the purple line"
When someone asks you to calculate Big-O, you do the below things and you are golden :
If you have to check no of times a string occurs in an array, you would go for a for loop which will loop through the elements and keep increment a counter, which has a complexity of O(N) where N is the number of inputs. Always think, what will happen when N reaches a million or a trillion. How will your code perform.
I have added the Big-O complexity to the code. Final complexity is 2*O(1) + 2*O(N)
If we consider the 1st rule, and ask the question, for a data input more than a million, the constants seems very small. For Example : lets say N = 1000000000
Now 2*O(1) is negligible 2*O(1000000000) and multiplying it with 2 is also not required as the number is too big. Hence in a world with such big number the other constant associated with can be removed.
Hence the Big-O is O(N)
So if you have two inputs A and B then Big-O for a Linear Time would be O(A+B)
If you see a nested loop, multiply the Inputs. These kind of Complexity is called Quadratic Time Complexity or O(N^2). If there are two inputs then O(A*B)
*More to come*
If you are wondering what it means let me be very straight forward to you.
"You have to write your algorithm with time and space complexity near or below the purple line"
When someone asks you to calculate Big-O, you do the below things and you are golden :
1. Worst Case Scenario
Always consider the worst case scenario. As yourself this, question a million times."What if the input is huge, like a million or a trillion ?"If you have to check no of times a string occurs in an array, you would go for a for loop which will loop through the elements and keep increment a counter, which has a complexity of O(N) where N is the number of inputs. Always think, what will happen when N reaches a million or a trillion. How will your code perform.
2. Remove Constants -
I have added the Big-O complexity to the code. Final complexity is 2*O(1) + 2*O(N)
If we consider the 1st rule, and ask the question, for a data input more than a million, the constants seems very small. For Example : lets say N = 1000000000
Now 2*O(1) is negligible 2*O(1000000000) and multiplying it with 2 is also not required as the number is too big. Hence in a world with such big number the other constant associated with can be removed.
Hence the Big-O is O(N)
3. Different Terms for inputs
Its simple, the complexity depends on the number of inputs you have to work on.So if you have two inputs A and B then Big-O for a Linear Time would be O(A+B)
4. Nested Is Always Multiply
If you see a nested loop, multiply the Inputs. These kind of Complexity is called Quadratic Time Complexity or O(N^2). If there are two inputs then O(A*B)
*More to come*
Comments
Post a Comment