【leetcode题解】【再看一遍】【86】【M】Contains Duplicate III
2015-12-17 23:08
363 查看
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:
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
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}
注释了的是原来的算法,想法不对,又太笨。
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 '''
相关文章推荐
- kaldi increasing GMM components in the training procedure
- 报错: Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串"
- 人工智能(AI)之朴素贝叶斯(NB)的基本实现
- 人工智能(AI)之KNN的基本实现
- Failed to import pydot 和graphviz error: (2, 'RegOpenKeyEx',错误总结
- CF 335 div.2-C/div.1-A/605A Sorting Railway Cars
- struct的初始化及其构造函数 http://blog.csdn.net/niuox/article/details/7253374
- hdu 1021 Fibonacci Again【找规律】
- http://a.codekk.com/detail/Android/grumoon/Volley%
- Daily Scrum 12.17
- leetcode刷题日记——Contains Duplicate
- 【leetcode题解】【99.38】【E】Contains Duplicate II
- 使用mailx发送邮件
- 开源项目学习笔记-Kuaihu(仿知乎日报)
- 什么是 Conversion to Dalvik format failed ?
- 从Container内存监控限制到CPU使用率限制方案
- UVa 11448 - Who said crisis?
- 再叙TIME_WAIT
- Codeforces Round #335 (Div. 1) A. Sorting Railway Cars
- 56: Recv failure: Connection was reset和55错误解决办法