leetcode-solutions icon indicating copy to clipboard operation
leetcode-solutions copied to clipboard

Added-cpp-solution

Open PraneshASP opened this issue 5 years ago • 5 comments
trafficstars

PraneshASP avatar Oct 01 '20 05:10 PraneshASP

I know this passes the test. but why this works? seems the complexity is n * n?

Yes it runs at n^2. I uploaded it as it will be easy to understand for beginners. I can also add the solution that has linear time complexity.

PraneshASP avatar Oct 02 '20 06:10 PraneshASP

I updated the code. You may check.

Thanks

PraneshASP avatar Oct 02 '20 06:10 PraneshASP

this works as well -- except that the run time complexity is bigger than

"Your algorithm should run in O(n) time and uses constant extra space."

this implies modifications of the input array IMO. without this constraint, this problem would be easy

franklingu avatar Oct 03 '20 05:10 franklingu

Ok.. I accept. Now you can check. I've updated the solution that runs in linear time and with constant space.

PraneshASP avatar Oct 05 '20 08:10 PraneshASP

it is actually still not o n since u r using sort

franklingu avatar Oct 10 '20 09:10 franklingu