DEV Community

Omi
Omi

Posted on

1

Leetcode's runtime "beats x%" is x% reliable

Introduction

It's been a while I have been solving Leetcode problems. I always notice that Leetcode says my code sucks by showing me the image given below.

Solution beats x%

Digging deep into the matter

Let's say you are solving a hard problem, which requires you to think about more optimal approach. You bring down the complexity from O(n^2) to O(nlogn) or better => O(n). Or just O(1) (Who knows lol!). And suddenly you went from beats 5% to beats 100%! Which is amazing. You actually solved the problem the right way!

But let's say you are just warming up. You know the problem will be solved in O(n) only. You code it the right way. Optimized and removed unnecessary calculations and still your solution beats 5%. It is taking around 500ms more.

When you look at the 100% beats solutions, they have done some nasty optimizations on low level (like, on reading input etc), but for the solutions which are just same as yours beat you. In that case, the runtime estimations become unreliable.

Conclusion

Look at the top solutions to know what low level optimizations are done and practice them. Learn to implement them. If you have something better, then congrats. Else, just move to the next problem.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
deep_gandhi_4070895b20965 profile image
Deep Gandhi β€’

as you say my friendly neighbourhood developer πŸ•ΈοΈπŸ’»

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ❀️ or leave a quick comment to share your thoughts!

Okay