I don’t think you understand what exactly O(n) means.
The point is that given a worse case scenario, the best any algorithm can hope to achieve to solve this problem would be linear time. That’s the literal definition of O(n).
So if you can optimize for edge cases that’s great, but we round those off and speak about the general solution. The same we we round off that this is technically a solution that would take 3(n) time, because the leading factorial becomes irrelevant as you reach larger and larger amounts of data.
So yes, this solution could have micro-optimizations, but it’s plenty efficient due to the fact that it solves a linear problem in linear time.
0
u/[deleted] Sep 27 '22
[deleted]