Getting a TLE in leetcode

I’ve written some code to solve this leetcode problem :- Loading...
Description of the problem:-
Given an array nums of n integers where nums[i] is in the range [1, n] , return an array of all the integers in the range [1, n] that do not appear in nums .

My problem :-
It works fine on small testcases it works fine, but on larger inputs it takes a lot of time and leetcode gives me a TLE. It passed the smaller testcases. How do I make my code more efficient?

My code :-

def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        # TLE :-
        noRange = range(1, (len(nums)+1))
        print("nums is ", nums)
        missingNos = []
        for no in noRange:
            if not (no in nums):
                 missingNos.append(no)
            else:
                continue;
        return missingNos

Thank you very much!! :grin:

Turns out if I set nums to set(nums) it won’t give me a TLE. Maybe because sets are faster at these things. Although I passed the test I’m open to feedback on how I can improve my function. My function is, still, slower than 80% of the submissions and uses more memory than 50% of submissions.

I looked some stuff up and turns out they ARE indeed faster than lists at these things.

They are faster becaus sets follow the concept of hash-tables and provide a time-complexity of O(1) → whereas arrays have O(n).

Now for understanding how you could improve, take a look at better code. On Leetcode when looking at the bar-charts, you can actually click on the bar and it shows the code that got this.

Also keep in mind that your machine has influence on the speed of LC tasks. So having a better machine and/or less resources used by programs, would slightly increase your rating.

1 Like

Oh, that’s why.

I don’t know of any bar-chart in leetcode. Could you please tell me where I can find that?

I thought they ran code on their servers.

Thanks a lot for the information. I’ll add sets to my list of things to decrease resource utilization :laughing:

If you submit code and it passes, you can click on the “Accepted” and get to the submision-detail page which shows a distribution as a bar chart of other submisions performance.

I just noticed that even copy-pasting the best solutions didn’t give me the best score and that having more programs running on my machine slightly worsened my score. I don’t actually know how they run the code.

1 Like

Printing the numbers is expensive. Side note, no is a not a great variable name for a number.

Other side note, many of the ‘best solutions’ on LeetCode are cryptic code that relies upon tricks. I wouldn’t rely much upon LeetCode if you want to become a professional developer.

1 Like

Wow, this tab is really helpful.

okay.

I will use print statments for testing only from now on.

:sweat_smile: I just named it without putting much thought into it.

Okay.

I love printing output for debugging. For bigger applications, I’ll set an environment variable that enables/disables printing so I don’t have to hunt down and comment out all of the print statements.

2 Likes

Wow, that’s a great idea. I just use regex search in vs code and remove all print statements.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.