Reputation: 2733
I have an algorithm (very basic), for which i have two solutions in mind, the complexities of two are : Approach 1 :
Space complexity: O(n)
Time Complexity : O(n)
Approach 2 :
Space complexity: O(1)
Time Complexity : O(nlogn)
which approach to opt for, i am looking for best practice in situation such as this.
Edit 1 : My input is infinitely large.
Upvotes: 0
Views: 472
Reputation: 1819
Algorithmic evaluation definitely depends on the problem.
For example your Approach 1 might be great if your n < 2^30
in which case your algorithm will use the rest of 2^30
bits in the space consumed.
You Approach 2 will be more scalable since it doesn't need any major additional memory. Its better for someone to wait a little longer for a result (nlogn
is better than n^2
) than crashing the system which is supposed to work.
So, it all depends on your requirement.
Upvotes: 1