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
 

The meaning of problems: if there are two elements of an array, the subscript does not exceed k at the same time does not exceed the difference between the value of t.

Honey can also traverse directly but O (n ^ 2) submitted only beat 10%.

Two sub-title encounter bucket method did not do it a little frustrated. . .

Provided two atoms a, b 
satisfying A - B <= T 
then (A -b) <T +. 1  
(A -b) / (T +. 1) <. 1 
A / (T +. 1) - B / (T 1) <1

 

T + 1 as it is used to divide the bucket size, then the two numbers in a bucket must be to meet the requirements, it may be adjacent to meet the requirements, need to be determined.

/**
 * @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;
};

Before there is a doubt bucket at a point if there are two numbers, then the   bucket [index] = nums [i ];   the latter in front of the figure is not yet covered. . . Since then expect direct returns in a bucket true of how so much. . .

 

And then there's a little simple approach, need the help of the library function, anyway, I can not write. . . Only use the 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 = 0; i < nums.size(); i++) {
            //Need to find a number between [the nums [I] -t, the nums [I] + T] 
            
            // > = [I] -t minimum value of the nums 
            Auto X = SET .lower_bound (( Long  Long ) the nums [I] - T);
             IF (! = X SET .end () && ABS (( Long  Long ) * X - the nums [I]) <= T) {
                 return  to true ; 
            } 
            SET .insert (the nums [I]);
             IF ( I> = K) {
                 SET .erase (the nums [I - K]); 
            } 
        } 
        return  to false ; 
    } 
};

Lower_bound use set of numbers can be found in the section closest to the target in logn time, and determine whether they meet the requirements.

 

Guess you like

Origin www.cnblogs.com/wenruo/p/11967644.html