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 ...
随机推荐
- MongoDB官方下载安装设置配置文件指定端口号
1.)下载 官网(https://www.mongodb.com/)右上角try free 进入下载中心,下载指定版本 ZIP和MSI随便 如果浏览器下载的慢,可以直接使用下载地址,然后迅雷下 操作 ...
- CSS教程详解
CSS学习笔记 一.CSS基础 1.CSS简介 层叠:一层一层的: 样式表:很多的属性和样式 CSS语法: <style> 选择器 { 属性名:属性值; 属性名:属性值; …… } &l ...
- vertx-mysql-client/java/
Reactive MySQL Client是MySQL的客户端,它具有直接的API,专注于可伸缩性和低开销. 特征 事件驱动 轻巧的 内置连接池 准备查询缓存 游标支持 行流 RxJava 1和RxJ ...
- IIS错误:在唯一密钥属性“fileExtension”设置为“.json”时,无法添加类型为“mimeMap”的重复集合项
在用visual studio 打开一个asp.net mvc 项目时,ctrl+f5运行,发现页面无法加载图片.js.json文件. 按F12查看错误,发现500错误.打开报错的js文件,提示: I ...
- js、jquery、css属性及出错集合
*)注意使用jquery设置css的语法 css("propertyname","value");#单个时时逗号 css({"propertyname ...
- jQuery-对列表的操作
主要是通过对dom元素的增加和删除实现对数据增加和删除 <!DOCTYPE html> <html lang="en"> <head> < ...
- 如何在unbuntu 16.04上离线部署openssh
背景:由于部署环境不能联网,为了方便文件传输,需要用到openssh.故实施步骤是,先在可以联网机器上下载离线包,然后用U盘拷贝到部署环境中. 第一步:下载离线包,下载网址:https://packa ...
- 如何在一个ubuntu系统上搭建SVN版本控制工具
有话说,由于公司项目部署需要,将Windows工程迁移到Linux,通过调查确定使用Ubuntu的Linux操作系统.那么如何快速搭建和Windows一样快捷方便的开发环境就很重要了.本文讲述如何在一 ...
- CentOS7 配置 SSH监听多个端口方法
一.修改ssh默认端口,防止暴力破解,让系统安全多一点点: i. 在配置文件/etc/ssh/sshd_config文件中修改 Port #AddressFamily any #ListenAddre ...
- springcloud学习之路: (三) springcloud集成Zuul网关
网关就是做一下过滤或拦截操作 让我们的服务更加安全 用户访问我们服务的时候就要先通过网关 然后再由网关转发到我们的微服务 1. 新建一个网关服务Module 2. 依然选择springboot工程 3 ...