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 ...
随机推荐
- Kubernetes 企业级集群部署方式
一.Kubernetes介绍与特性 1.1.kubernetes是什么 官方网站:http://www.kubernetes.io • Kubernetes是Google在2014年开源的一个容器集群 ...
- Prometheus 监控K8S集群资源监控
Prometheus 监控K8S集群中Pod 目前cAdvisor集成到了kubelet组件内,可以在kubernetes集群中每个启动了kubelet的节点使用cAdvisor提供的metrics接 ...
- 来迟了,用Python助你叠猫猫,抢618大红包!
目录: 0 引言 1 环境 2 需求分析 3 前置准备 4 逛店铺流程回顾 5 代码全景展示 6 总结 0 引言 最近叠猫猫的活动可真是十分的火爆,每天小伙伴们为了合猫猫忙的可谓是如火如荼.为啥要叠猫 ...
- 2018-8-10-WPF-checkbox文字下掉
原文:2018-8-10-WPF-checkbox文字下掉 title author date CreateTime categories WPF checkbox文字下掉 lindexi 2018- ...
- Delphi - 16进制取反 Not
//Not直接实现十六进制取反var I, J : word; begin I := $96E5; J := Not I; ShowMessage(Format('%x',[J])); end; 作者 ...
- Python struct与小端存储
参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017685387246080 在使用Python 实现字符向字节数据类型转换的时候,P ...
- python类模拟电路实现
实现电路: 实现方法: class LogicGate(object): def __init__(self, n): self.name = n self.output = None def get ...
- Bootstrap-table表格插件的使用方法
前言 上次写了一个可拖动列表的插件,但除了这个特点没什么优点了,接下写的是我们常用的Bootstrap-table表格插件 正文 官网:https://bootstrap-table.com/ 有两个 ...
- Xml格式数据转map工具类
前言[ 最近在写一个业务,由于调UPS物流的接口返回的是一个xml格式的数据结果,我现在要拿到xml中某个节点的值,而这个xml数据是多节点多层级的,并且这某个节点的值有可能只有一条值,有可能有多条, ...
- Linux(Centos7)搭建LAMP(Apache+PHP+Mysql环境)
目录 Linux搭建LAMP(Apache+PHP+Mysql环境)Centos7 一. 检查系统环境 1.确认centos版本 2.检查是否安装过apache 3.检查是否安装过Mysql 4.清理 ...