Should you relax the memory limit a little for ruby?

  • 0

    I use array with length of n/2 for sieve, and got MLE every time.
    I tried same logic with Python and it's accepted. Actually, according to posts at discussion, I saw Python code with n length list got accepted.
    I assume Python and Ruby should not have similar limits, right?
    Are you too strict to rubiers?


    # @param {Integer} n
    # @return {Integer}
    def count_primes(n)
    return 0 if [0, 1, 2].include? n
    upper = n / 2
    stop = (Math.sqrt(n) - 1) / 2
    sieve = {true}
    x = 1
    while x <= stop
      if sieve[x]
        step = 2 * x + 1
        s = x + step
        while s < upper
          sieve[s] = false
          s += step
      x += 1
    sieve.count {|x| x}

  • 0

    Thanks for your feedback. I've relaxed the memory requirements for Ruby a little, and your code now get Accepted.

Log in to reply

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