time complexity
Hi I have a question that:
consider I have T(n) = m * n^2 (n<m)
is this correct to write T(n) = O(m)
? because I have written T(n) = m*n*n
So bec开发者_开发技巧ause n<m
we have T(n) = O(m)
thanks
No, the best thing you can do is to write T(n,m) = O(m^3)
. n < m
is a very weak condition and basically just gives you n in O(m)
. For example, n could always be m-1
.
Edit: My first answer was imprecise, as T was only a function in n. If m is constant, the answer is still holds, but O(m^3) is equal to O(1).
I believe in this case T(n) = O(n^2)
The formal definition of big-O:
f(x) = O(g(x)) if and only if there exists a positive real number M and a real number x0 such that |f(x)| ≤ M|g(x)| for all x > x0.
In your case, T(n) will always be less than or equal to m(n^2).
精彩评论