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.
Am I wrong that this solution is > O(n)? This problem can be done in linear time, no? Python's max() is O(n) and you call it twice each iteration and only luckily reduce n by 1 each iteration. The set [2,2,2,2,2] would have like 30 comparisons to find max plus 5 for your z initialization and then 0 for the print I believe. 5+5+4+4+3+3+2+2+1+1+0+0
[1,2] would be 2+2+1
[1,7,3] would be 3+3+2
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Runner-Up Score!
You are viewing a single comment's thread. Return to all comments →
Am I wrong that this solution is > O(n)? This problem can be done in linear time, no? Python's max() is O(n) and you call it twice each iteration and only luckily reduce n by 1 each iteration. The set [2,2,2,2,2] would have like 30 comparisons to find max plus 5 for your z initialization and then 0 for the print I believe. 5+5+4+4+3+3+2+2+1+1+0+0 [1,2] would be 2+2+1 [1,7,3] would be 3+3+2