Example #1
0
    def containsNearbyAlmostDuplicate(self, nums: List[int], k: int,
                                      t: int) -> bool:
        if t < 0:
            return False
        s = SortedList()
        for i in range(0, len(nums)):
            # print(s)
            if i > k:
                numToDelete = nums[i - k - 1]

                s.remove(numToDelete)
            # print(s)
            if s.__contains__(nums[i]):
                # print("yea")
                return True
            s.add(nums[i])
            pos = s.index(nums[i])
            # print(s, pos)
            if pos > 0:
                tmp = s.__getitem__(pos - 1)
                # print(tmp)
                if abs(nums[i] - tmp) <= t:
                    return True
            if pos < len(s) - 1:
                tmp = s.__getitem__(pos + 1)
                # print(tmp)
                if abs(nums[i] - tmp) <= t:
                    return True

        return False