How to determine memory and time complexity of an algorithm?

前端 未结 2 506
耶瑟儿~
耶瑟儿~ 2020-12-23 19:44

I am not good at determining time and memory complexities and would appreciate it if someone could help me out.

I have an algorithm, here and I am not sure what its

2条回答
  •  时光取名叫无心
    2020-12-23 20:33

    You're really looking at log_2(k), logarithm with base 2. To change bases, you have to multiply by a constant. And since we multiply by constants anyways, O(log(n)), O(ln(n)), and O(log_2(n)) are all the same.

    So why does the above method have logarithmic complexity with base 2? You're dividing k in half at every call. If you go backwards, you're multiplying by 2 at every call. Multiplying by 2 is 2^n, and log_2(n) is exactly the inverse of that.

    Maybe it helps if you draw a binary tree: a tree with n nodes has height log_2(n), a tree with height n has 2^n nodes.

提交回复
热议问题