220. Contains Duplicate III 数组指针差k数值差t
[抄题]:
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3, t = 0
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1, t = 2
Output: true
Example 3:
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
bucket值可能很小,所以用 让分子变大。
- Integer.MIN_VALUE
[思维问题]:
以为2个指针,没想到果然有技巧
[英文数据结构或算法,为什么不用别的数据结构或算法]:
水桶排序的精髓在于容量恒定,重复出现的放在下一个水桶。此题要求数字差值恒定,所以可以用水桶排序。
做法:/容量即可。类似取余数。
[一句话思路]:
差值不超过t, 而且越近越好。在k范围内的bucket值重复出现肯定可以,相邻的检查一下也可以。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- map里存的是对象,变量类型都是大写开头。只能用put方法。其他一般的变量类型用小写开头即可。
long remappedNum = (long) nums[i] - Integer.MIN_VALUE;每个变量都要改成long- 求map整个一坨的方法是:
if (map.keySet().size() > k)
[二刷]:
long型放在分母时,要加括号
/ ((long)t + 1);
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
nums[i - k] 要不要-1 自己试试就行了,也是debug的一个重点
[总结]:
水桶排序要求不能重复。做法:/容量即可。类似取余数。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
//cc
if (nums == null || nums.length == 0) return false;
if (k < 1 || t < 0) return false;
//ini: HashMap, bucket
HashMap<Long, Long> map = new HashMap<>();
//for loop: judge as true in 3 cases, add(bucket, value) to map while maintain k
for (int i = 0; i < nums.length; i++) {
long appendedNum = (long)nums[i] - Integer.MIN_VALUE;
long bucket = appendedNum / ((long)t + 1);
//judge as true in 3 cases
if (map.containsKey(bucket) ||
(map.containsKey(bucket - 1) && Math.abs(appendedNum - map.get(bucket - 1)) <= t) ||
(map.containsKey(bucket + 1) && Math.abs(map.get(bucket + 1) - appendedNum) <= t)) return true;
//while maintain k numbers in the map
if (map.entrySet().size() >= k) {
long lastBucket = ((long)nums[i - k] - Integer.MIN_VALUE) / ((long)t + 1);
map.remove(lastBucket);
}
//put to map
map.put(bucket, appendedNum);
}
return false;
}
}
220. Contains Duplicate III 数组指针差k数值差t的更多相关文章
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
二分.情况讨论 因为数组有序,所以能够考虑用二分.通过二分剔除掉肯定不是第k位数的区间.如果数组A和B当前处理的下标各自是mid1和mid2.则 1.假设A[mid1]<B[mid2], ①.若 ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- [Leetcode] 220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- LeetCode 220. Contains Duplicate III (分桶法)
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [刷题] 220 Contains Duplicate III
要求 给出整型数组nums和整数k,是否存在索引i和j 使得nums[i]和nums[j]之差不超过t,且i和j之差不超过k 思路 建立k个元素的有序查找表 每次有新元素加入,寻找查找表中大于 num ...
- (medium)LeetCode 220.Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
随机推荐
- PythonStudy——列表的常用操作 List of common operations
# 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...
- MySQL Execution Plan--NOT EXISTS子查询优化
在很多业务场景中,会使用NOT EXISTS语句来确保返回数据不存在于特定集合,部分场景下NOT EXISTS语句性能较差,网上甚至存在谣言"NOT EXISTS无法走索引". 首 ...
- IDEA整合Junit测试框架
首先说一下为什么会有这篇文章吧,百度没有找到一个我想要的答案.也可以理解为,作为一个java菜鸟,一个对idea和jar理解不深的人,想跟着博客一步步操作完之后发现,哎不行,之后的牢骚吧.主要是为了记 ...
- Java day1
1. 学习java,首先是jdk的安装,JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+ ...
- 用UpdateResource修改EXE文件图标(已修正)
//请自行添加到 Type 处PICONDIRENTRY = ^ICONDIRENTRY;ICONDIRENTRY = packed record bWidth: Byte; bHeight: Byt ...
- 【rabbitmq】Centos7 下安装rabbitmq
rabbitmq安装 rabbitmq的安装依赖erlang,首先应该先安装erlang,然后安装rabbitmq: Step1:安装erlang erlang-rpm安装教程 选择在Centos7 ...
- linux服务之apache篇(一)
1.apache介绍:使用率最高的网站服务器: URL:统一资源定位符: 端口:http:80 https:443 2.apache三种工作模式: prefork:一个线程处理一个请求(占用内存多 ...
- gentoo 图像方面的软件
图像方面的软件一般包括:查看图像,屏幕截图,图像修改. 查看图像简单的可以安装 feh,但是 feh 一般作为墙纸来用.稍微好一些的是 gqview. 屏幕截图可以用 screengrab,使用的时候 ...
- python基础之从认识python到python的使用
python的历史: python的创始人是吉多·范罗苏姆(Guido van Rossum),人称“龟叔”,1989年圣诞节期间,Guido开始写Python语言的编译器.他希望这个叫做Python ...
- 英文文档帮查&翻译计划
以CSDN为首,知乎其次,cnblog带路的一大批博客上充斥着大量低质量的编程入门教程,代码粗制滥造,毫无缩进,没有高亮,东抄西抄.初学者如果长期参照这种垃圾博客来解决问题,将会适得其反,走入歧途. ...