Python Dictionary. What is wrong with my iterables


  • 0
    V

    def containsNearbyAlmostDuplicate(self, nums, t, k):

        d = dict()
        for i in range(len(nums)):
            if d.get(abs(nums[i] - p) for p in range(t)) is not None:
                if i - d[nums[i] - p] <= k:
                    return True
            if d.get(abs(nums[i] + z) for z in range(t)) is not None:
                if i - d[nums[i] + z] <= k:
                    return True
            d[nums[i]] = i
        return False

  • 0
    Z

    It exceeds the limit.

    Your complexity is O(tn), if t>> n, it is less efficient than an O(n^2) algorithm

    So simply consider when to iterate over t, when to iterate over k, should solve the problem


Log in to reply
 

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