Understanding Big O notation - Cracking the Coding Interview

前端 未结 3 1201
悲哀的现实
悲哀的现实 2021-02-05 10:17

I need help understanding how the author got the answer of problem 11 in the Big O chapter.

The problem goes like this:

The following code prints

3条回答
  •  清酒与你
    2021-02-05 11:11

    Printing the string is only an extra addition to the k time.

    Checking whether each string is sorted is O(k) and say that printing it is O(dk) for some integer d (a constant). Adding the two we get O(k + dk), which can be re written as O(k(1 + d)). Because this just a scalar we know O(k + dk) = O(k) so the answer does not change.

提交回复
热议问题