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

175 篇文章 0 订阅
157 篇文章 0 订阅

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]
3
1
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值