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 (分桶法)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. (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 ...

  4. [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 ...

  5. 【LeetCode】220. Contains Duplicate III

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

  6. 220. Contains Duplicate III

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

  7. POj 2104 K-th Number (分桶法+线段树)

    题目链接 Description You are working for Macrohard company in data structures department. After failing ...

  8. 220 Contains Duplicate III 存在重复 III

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...

  9. 【medium】220. Contains Duplicate III

    因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...

随机推荐

  1. IDEA快速修复错误快捷键

    有的时候在IDEA中编写代码,会出现错误提示,比如需要处理异常 将光标移动到出错,也就是划红线的地方,行首会出现一个小灯泡,点击会出现图二,可以按照提示进行修复

  2. 一个人的公众号,我写了1w+

    大家好,我是Bypass,一个人一直保持着写博客的习惯,为此维护了一个技术公众号,致力于分享原创高质量干货,写的内容主要围绕:渗透测试.WAF绕过.代码审计.应急响应.企业安全. 一直以来,我把它当成 ...

  3. 禁用software reporter tool.exe 解决CPU高占用率的问题

    或者 或者 C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\SwReporter\36.184.200 下编辑 manifes ...

  4. PIE属性表多字段的文本绘制

    最近研究了PIE SDK文本元素的绘制相关内容,因为在我们的开发中,希望可以做到在打开一个Shp文件后,读取到属性表的所有字段,然后可以选择一些需要的字段,将这些字段的所有要素值的文本,绘制到shp图 ...

  5. 本机与虚拟机Ping不通

    关闭防火墙,设置虚拟机和本机在同一网段,还是ping不同 解决方法:在VMware中点击 编辑---->虚拟网络编辑器----->更改设置 ------->还原默认设置 然后重新配置 ...

  6. Linux用户和权限——管理文件权限的命令

    Linux用户和权限——管理文件权限的命令 摘要:本文主要学习了Linux中修改文件权限的命令. chown命令 chown命令,主要用于修改文件(或目录)的所有者,除此之外,这个命令也可以修改文件( ...

  7. JVM 学习总结

    目录 Java内存区域 运行时数据区 & Java 内存结构 & Java 内存区域 1. 程序计数器 2. Java 虚拟机栈 3. 本地方法栈 4. 堆 5. 方法区 6. 运行时 ...

  8. 在React中使用react-router-dom路由

    1,路由组件的基本实现 使用React构建的单页面应用,要想实现页面间的跳转,首先想到的就是使用路由.在React中,常用的有两个包可以实现这个需求,那就是react-router和react-rou ...

  9. 基础系列(2)--- css1

    css组成 css语法组成 选择器 和 声明 (多个声明用分号隔开) 声明包括 属性和属性值(多个属性值用空格隔开) 语法: 选择器{ 属性: 属性值; 属性: 属性值1 属性值2; } css样式表 ...

  10. boa移植 boa交叉编译

    官网:http://www.boa.org/ BOA 服务器是一个小巧高效的web服务器,是一个运行于unix或linux下的,支持CGI的.适合于嵌入式系统的单任务的http服务器,源代码开放.性能 ...