Reputation: 145
I am trying to develop more understanding about running times.
Suppose I have the code inside my function, and each statement has some different time complexities:
LinkedList myLL = new LinkedList(); //O(1)
myLL.addAtHead("1"); //O(1)
myLL.addAtHead("2"); //O(1)
myLL.addAtHead("3"); //O(1)
int[] myArray = new int[n] //O(n) , depending on what n is
<Some other statement> //O(n^2)
Determining the running time, it is O(n^2) ? Do we just consider statement that takes the most time and say that the total running time is O(n^2)?
Upvotes: 0
Views: 24
Reputation: 27238
Yes, the prevailing term cancels all the other ones (well, it makes them insignificant in the overall consideration), so, the complexity is O(n^2).
Upvotes: 1