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.

这个问题在前一个问题的基础上又对nums[i],nums[j]的关系作了约束,要求|nums[i]-nums[j]|<=t && |i-j|<=k;

对于这个问题,在前面代码的基础上,一个很自然的做法是:

 public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
Map<Integer,Integer> map = new HashMap<>(0); for(int i=0; i<nums.length; i++){
for(int num : map.keySet()){
if(Math.abs(num-nums[i])<t && i-map.get(num)<k) return true;
}
map.put(nums[i],i);
if(map.size()>k) map.remove(nums[i-k]);
}
return false;
}
}

这是一个两层的循环,对于每一个nums[i],把它与Map中包含的其他所有元素进行比较,检查是否有满足条件的元素。这个方法很容易理解,但是时间复杂度太高,如果k很大的话,内层循环的迭代次数会很多。导致Time Limit Exceeded。

如何才能对该算法进行改进呢?在参考了别人的代码后,发现了一个很机智的解法,它的时间复杂度是O(n),先把代码贴出来如下:

 public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (t < 0) return false;
Map<Long, Long> d = new HashMap<>();
long w = (long)t + 1;
for (int i = 0; i < nums.length; ++i) {
long m = getID(nums[i], w);
if (d.containsKey(m))
return true;
if (d.containsKey(m - 1) && Math.abs(nums[i] - d.get(m - 1)) < w)
return true;
if (d.containsKey(m + 1) && Math.abs(nums[i] - d.get(m + 1)) < w)
return true;
d.put(m, (long)nums[i]);
if (i >= k) d.remove(getID(nums[i - k], w));
}
return false;
} private long getID(long i, long w) {
return i < 0 ? (i + 1) / w - 1 : i / w;
}
}

接下来我们对该算法进行分析。首先我们来看下getID函数的功能。

i>=0时,ID是i/w。举个例子,假设t=2,则w=3。如果nums[] = {0,1,2,3,4,5,6,7}。

nums[0]/w = 0/3 = 0;

nums[1]/w = 1/3 = 0;

nums[2]/w = 2/3 = 0;

nums[3]/w = 3/3 = 1;

nums[4]/w = 4/3 = 1;

nums[5]/w = 5/3 = 1;

nums[6]/w = 6/3 = 2;

这样就把nums[i]按照值的不同划分到了不同的组中。如果存在两个数|nums[i]-nums[j]|<=t,那么这两个数的ID或者相同,或者至多相差1。

同样i<0时,ID是(i+1)/w - 1。同样假设w=3,nums[]={-6,-5,-4,-3,-2,-1}。

nums[0]/w = (-6+1)/3 -1 = -3;

nums[1]/w = (-5+1)/3 -1 = -3;

nums[2]/w = (-4+1)/3 -1 = -2;

nums[3]/w = (-3+1)/3 -1 = -2;

nums[4]/w = (-2+1)/3 -1 = -2;

nums[5]/w = (-1+1)/3 -1 = -1;

这样就把nums[i]按照值的不同划分到了不同的组中。如果存在两个数|nums[i]-nums[j]|<=t,那么这两个数的ID或者相同,或者至多相差1。

可以发现,这两个划分是连续的。ID=···-n,···,-1,0,1,···,n···。

那么对于每一个nums[i],先计算它的ID,然后在Map中判断是否包含此ID,如果包含,那么肯定存在这样的两个数,他们之间的差小于等于t。否则的话判断是否存在与此ID相差为1的ID,即ID-1,ID+1。如果存在的话,判断两个数之间的差是否小于w(即小于等于t)。对于其他的ID,其实是没有必要来判断的。可以看到,提前计算ID的过程使得对于每一个nums[i],不需要像上一个算法中对Map中的每个数进行比较,而最多只需要进行三次比较即可判断出是否有满足条件的元素。

LeetCode OJ 220.Contains Duplicate 3的更多相关文章

  1. LeetCode OJ 219.Contains Duplicate 2

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. LeetCode OJ 217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  3. 【LeetCode】220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode OJ:Contains Duplicate III(是否包含重复)

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  5. LeetCode OJ:Contains Duplicate(是否包含重复)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  6. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  7. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  8. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  9. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

随机推荐

  1. QT自绘标题和边框

    在QT中如果想要自绘标题和边框,一般步骤是: 1) 在创建窗口前设置Qt::FramelessWindowHint标志,设置该标志后会创建一个无标题.无边框的窗口. 2)在客户区域的顶部创建一个自绘标 ...

  2. C++笔记(to be cont'd)

    最近在看这个:LearnCpp 主要是一些我自己容易忽视的地方 记一些笔记在下面,还有错漏地方请不吝赐教 CH1.10 Preprocessing The preprocessor copies th ...

  3. 使用MegaCli工具,在线调整raid配置

    公司hadoop平台采购了一批浪潮服务器,2个系统盘,12个数据盘.先想的用直通的raid卡免得再做单盘raid0麻烦,结果这批机器配的卡也不支持裸盘使用,咨询浪潮客服,说可以使用JBOD的模式进行, ...

  4. HDU:3368-Reversi(暴力枚举)

    Reversi Time Limit: 5000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. linux安装包资源库

    最近发现了一个很不错的linux的rpm资源库,可以在里面找到rpm安装过程中缺失的资源! 网址:http://pkgs.org/

  6. WITH common_table_expression

    Feature: 公用表表达式只能包含一个SELECT,多SELECT需UNION,UNION ALL 公用表表达式只能引用一次 公用表表达式可以包括对自身的引用,这种表达式称为递归公用表表达式 -- ...

  7. iOS开发关于xcode中souceControl的苹果文档翻译(节选)

    Subversion 1.7 provides many benefits: svn1.7版本有以下好处: Improved performance. Increased speed for many ...

  8. Sun jdk, Openjdk, Icedtea jdk关系

    转自: http://blog.chinaunix.net/uid-20648944-id-3204527.html Sun jdk与Openjdk版本发展历史如下图所示: 1.    Openjdk ...

  9. CentOS/RedHat rpm方式安装Apache2.2

    注:所有RPM包均从网易镜像上下载 # rpm -ivh /home/apache/apr-1.3.9-5.el6_2.x86_64.rpm warning: /home/apache/apr-1.3 ...

  10. hdu_5879_Cure(打表)

    题目链接:hdu_5879_Cure 题意: 给你一个n,让你计算1/k2的和,k从1到n. 题解: 因为只保留5位小数,所以打个100W的表,比这个数大的直接输出最后一位就行了 #include&l ...