【leetcode题解】【再看一遍】【86】【M】Contains Duplicate III

来源:互联网 发布:gta5渣电脑神优化20桢 编辑:程序博客网 时间:2024/06/05 00:15

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.


Subscribe to see which companies asked this question




The idea is like the bucket sort algorithm. Suppose we have consecutive buckets covering the range of nums with each bucket a width of (t+1). If there are two item with difference <= t, one of the two will happen:
(1) the two in the same bucket(2) the two in neighbor buckets

Note that we do not need to actually allocate a lot of buckets. At any time there will only be at most min(k, n) buckets. All we need to do is calculate the label of the bucket m = value/(t+1), and check the buckets m - 1, m, m + 1. The whole algorithm is then O(n).


借鉴桶排序的的思想+滑动窗口

如果: | nums[i] - nums[j] | <= t   式a

等价: | nums[i] / t - nums[j] / t | <= 1   式b

推出: | floor(nums[i] / t) - floor(nums[j] / t) | <= 1   式c

​等价: floor(nums[j] / t) ∈ {floor(nums[i] / t) - 1, floor(nums[i] / t), floor(nums[i] / t) + 1} 式d

运行结果长这个样子
Your input
[1,3,111,11114,111115,1111111,11111112,11111113]31
Your stdout
{}
{0: 1}
{0: 1, 1: 3}
{0: 1, 1: 3, 55: 111}
{0: 1, 1: 3, 5557: 11114, 55: 111}
{1: 3, 55557: 111115, 5557: 11114, 55: 111}
{555555: 1111111, 55557: 111115, 5557: 11114, 55: 111}
{555555: 1111111, 5555556: 11111112, 55557: 111115, 5557: 11114}



注释了的是原来的算法,想法不对,又太笨。

class Solution(object):    def containsNearbyAlmostDuplicate(self, nums, k, t):        if t < 0:            return False        l = len(nums)        d = {}        t += 1        # 1/3 = 0        #-1/3 = -1        for i in xrange(l):            print d            if i > k:                del d[nums[i - k - 1] / t]# 维护一个大小为k+1的桶            m = nums[i] / t            if m in d:                return True            elif m-1 in d and abs(nums[i] - d[m-1]) < t:                return True            elif m+1 in d and abs(nums[i] - d[m+1]) < t:                return True            d[m] = nums[i]        return False        '''        l = len(nums)        if not nums:            return 0 < k        if l == 1 or k == 0 or t < 0:            return False        if l <= k:            for i in range(0,l-1):                if abs(nums[i+1] - nums[i]) <= t:                    return True            return False        i = k - 1        df = [0]*(k )        temp = nums[:k]        temp.sort()        for ii in range(0,k):            print ii            if ii == l-1:                break            df[ii] = (temp[ii+1]-temp[ii])        #df.sort()        print df        minn = min(df)        if minn <= t:            return True        print df        while i < l-1:            i += 1            temp_df = abs(nums[i]-nums[i-1])            if temp_df < minn:                df.remove(minn)                minn = temp_df                df.append(minn)            #print df            if minn <= t:                return True        return False        '''


0 0
原创粉丝点击