We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It is not true. I would like to mention few things to not mislead anyone.
• O(n) is same as O(2n), please refer definition in case of confusion.
• n refers to the size of input. The for-loop is O(n) because running time varies as the size of input "n" changes.
• list.index() is not O(n), but O(1). The index method is applied on a constant size (size=6) list. It can also be understood by the fact that its running time is independent of the size of input n.
• It is not preferred to have so many conditions inside any loop when they can be avoided. Though it might not change complexity, yet it will definitely slow it down.
• This solution is, in fact, slower than the solution in the main post because of the above mentioned reasons. You can verify it yourself. On my machine it is always slower by 40,000 micro-seconds or more for max input, which is considerable duration in terms of computer.
Now, for the coders learning from discussions, I would suggest to give importance to "readability" of the code. It matters a lot in professional environment. Though, here the scenario is reverse, but otherwise also it isn't adviced to compromize with readability to gain few micro seconds. "Zen of Python" gives a nice guideline, you can enter the following in python interpreter to read it:
importthis
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Migratory Birds
You are viewing a single comment's thread. Return to all comments →
It is not true. I would like to mention few things to not mislead anyone.
• O(n) is same as O(2n), please refer definition in case of confusion.
• n refers to the size of input. The for-loop is O(n) because running time varies as the size of input "n" changes.
• list.index() is not O(n), but O(1). The
index
method is applied on a constant size (size=6) list. It can also be understood by the fact that its running time is independent of the size of input n.• It is not preferred to have so many conditions inside any loop when they can be avoided. Though it might not change complexity, yet it will definitely slow it down.
• This solution is, in fact, slower than the solution in the main post because of the above mentioned reasons. You can verify it yourself. On my machine it is always slower by 40,000 micro-seconds or more for max input, which is considerable duration in terms of computer.
Now, for the coders learning from discussions, I would suggest to give importance to "readability" of the code. It matters a lot in professional environment. Though, here the scenario is reverse, but otherwise also it isn't adviced to compromize with readability to gain few micro seconds. "Zen of Python" gives a nice guideline, you can enter the following in python interpreter to read it: