Python Time Limit Exceeded


  • 0
    M

    I think the following solution is quite efficient still I get time limit exceeded.
    Any chances of relaxing the TLE or any suggestions on how to reduce the running time of my implementation?

    def countPrimes(self, n):
            if n < 2: return 0
            sieve = [1] * (n-1)
            ptr = 2
            while ptr < len(sieve) - 1:
                if sieve[ptr] == 0: 
                    ptr = ptr + 1
                    continue
                new_ptr = ptr + ptr
                while new_ptr < len(sieve): 
                    sieve[new_ptr] = 0
                    new_ptr = new_ptr + ptr
                ptr += 1
            return sum(sieve) - 2

  • 0
    S

    I am getting similar error "Time limit exeeds". However, I have checked out my code until about 5 million which runs reasonably fast, within few seconds. I haven't heard from anyone on the discussion forum yet.


  • 0
    M

    I see.. I actually pruned it down and this runs reasonably fast. I don't think we should be penalized for using "a slower language". Since, this is a new problem, I am sure the time limit will be fixed soon by the authors.
    Note to self: don't jump too quickly on problems which are recently added to leetcode.


  • 0
    J

    what is your running time for case 999983? I guess that's the problem of language


  • 0
    M

    In my case, the code runs till input 1500000


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.