Python solution - Time Limit Exceeded

  • 3

    I'm trying to improve this so it will pass the time limit constraint, but I don't know where this can be modified. Any suggestions? Thanks!

    class Solution:
        # @param {integer} n
        # @return {integer}
        def countPrimes(self, n):
            if n < 2:
                return 0
            seive = [True] * n
            seive[0] = False
            seive[1] = False
            i = 2
            while i * i < n:
                if seive[i]:
                    j = i
                    while i * j < n:
                        seive[i * j] = False
                        j += 1
                i += 1
            return sum(seive)

  • 0

    Sorry I've readjusted the time limit for Python. Please try submitting again, your code should get Accepted now.

  • 0

    Hi, I have the exactly same algorithm, but still get TLE on the input 1500000

  • 0

    same here, but indeed it is slow in LP shell

Log in to reply

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