Why is this solution exceeding the time limit?


  • 2
    A

    class NumArray(object):

    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.nums=nums
    
    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        return sum(self.nums[i:j+1])

  • 0
    V

    well it has multiple call on the method sumRange, each is going to take j-i steps. so the time complexity is number of calls * numbers of steps per call. Therefore its roughly n^2 complexity. I believe it wants less than n^2.


Log in to reply
 

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