LeetCode 220. Contains Duplicate III (分桶法)
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
题意:一个数组是否存在两个元素,下标差不超过 k 同时值之差不超过 t 。
蜜汁直接遍历也能通过 不过是 O(n^2) 只能打败 10% 的提交。
两次遇到分桶法的题都没做出来 有点挫败。。。
设两个数为 a,b
满足 a - b <= t
则 (a-b) < t + 1
(a-b)/(t+1) < 1
a/(t+1) - b/(t+1) < 1
所以使用 t+1 作为大小来进行分桶,那么在一个桶的两个数一定是符合要求的,相邻的话可能是符合要求的,需要进行判断。
/**
* @param {number[]} nums
* @param {number} k
* @param {number} t
* @return {boolean}
*/
var containsNearbyAlmostDuplicate = function(nums, k, t) {
if (k < 1 || t < 0 || nums.length < 2) {
return false;
}
let bucket = {};
for (let i = 0; i < nums.length; i++) {
let index = Math.floor(nums[i] / (t + 1));
if (bucket[index] != null) return true;
if (bucket[index - 1] != null && Math.abs(bucket[index - 1] - nums[i]) <= t) {
return true;
}
if (bucket[index + 1] != null && Math.abs(bucket[index + 1] - nums[i]) <= t) {
return true;
}
bucket[index] = nums[i];
if (i >= k) {
bucket[ Math.floor(nums[i-k] / (t + 1)) ] = null;
}
}
return false;
};
之前有一点处疑惑就是一个桶如果有两个数,那么 bucket[index] = nums[i]; 后面的数字不是把前面的覆盖了么。。。后来想到既然在一个桶直接就返回true了哪有这么多事。。。
然后还有简单一点的做法,需要借助库函数,反正我写不出。。。只能用 cpp 了。。。。
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <cmath> using namespace std; class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
set<long long> set;
for (int i = ; i < nums.size(); i++) {
// 需要找到一个数字在 [ nums[i]-t, nums[i]+t ] 之间 // >= nums[i]-t 的最小值
auto x = set.lower_bound((long long)nums[i] - t);
if (x != set.end() && abs((long long)*x - nums[i]) <= t) {
return true;
}
set.insert(nums[i]);
if (i >= k) {
set.erase(nums[i - k]);
}
}
return false;
}
};
使用 set 的 lower_bound 可以在 logn 时间内找到区间中与目标最接近数字,并判断其是否符合要求。
LeetCode 220. Contains Duplicate III (分桶法)的更多相关文章
- [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 ...
- Java for 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 ...
- (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 ...
- [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
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- POj 2104 K-th Number (分桶法+线段树)
题目链接 Description You are working for Macrohard company in data structures department. After failing ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
随机推荐
- date——系统时间的命令
这是一个可以用各种姿势获得各种时间的命令.最近在写自动化定时脚本时学了一下. 参考:https://www.cnblogs.com/ginvip/p/6357378.html 比如: 利用cronta ...
- Python中的常见特殊方法—— del方法
__del__() 方法用于销毁Python对象——在任何Python对象将被系统回收的时候,系统都会自动调用这个方法.但是不要以为对一个变量执行del操作,该变量引用的对象就会被回收,当然不是,如果 ...
- Vue 动态修改data 值 并触发视图更新
Vue 动态修改data 值 并触发视图更新 this.$set(obj, key, '') // Vue 动态修改或者添加data key 并触发视图更新
- 先排序然后union all失效,mysql数据库多个表union all查询并排序的结果为什么错误
mysql数据库多个表union all查询并排序的结果为什么错误? 群主,我想进行一个表的查询,先把表中某个字段的内容查出,然后其他的再排序,我用union all连接两个表的查询结果排序是错的 比 ...
- jQuery遍历 - 过滤first(),last()和eq()使用
jQuery遍历 - 过滤最基本的过滤方法是first(),last()和eq(),它们允许您根据元素在一组元素中的位置选择特定元素. 其他过滤方法(如filter()和not())允许您选择与特定条 ...
- 【Mysql】初识MySQL
一. MySQL是客户端/服务器架构1)macOS操作系统上的默认安装目录:/usr/local/mysql/ 在MySQL的安装目录下有一个bin目录,这个目录下存放着许多可执行文件.2)将该bi ...
- C语言的暂停
#include<stdio.h> int main(void) { printf("Hello, World!\n"); system("pause&quo ...
- jmeter压测学习8-压测带token的接口
前言 工作中我们需要压测的接口大部分都是需要先登陆后,带着token的接口(或者带着cookies),我们可以先登陆获取token再关联到下个接口. 比如我现在要压测一个修改用户的个人信息接口,每个用 ...
- 树莓派搭建SVN服务器
1.安装SVN服务器 sudo apt-get install subversion -y 2.创建仓库 mkdir /home/pi/svnRepository svnadmin create /h ...
- 201871010112-梁丽珍《面向对象程序设计(java)》第十一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...