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

Approach #1: C++. [Brute Froce]

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        int len = nums.size();
        for (int i = 0; i < len; ++i) {
            for (int j = i + 1; j - i <= k && j < len; ++j) {
                if (abs((long)nums[j] - (long)nums[i]) <= (long)t) return true;
            }
        }
        return false;
    }
};

  

Approach #2: Java. [TreeSet]

class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if (nums == null || nums.length == 0 || k <= 0)
            return false;
        
        TreeSet<Integer> values = new TreeSet<>();
        for (int i = 0; i < nums.length; ++i) {
            if (i > k) values.remove(nums[i-k-1]);
            Integer ceiling = values.ceiling(nums[i]);
            Integer floor = values.floor(nums[i]);
            if ((ceiling != null && (long)ceiling - (long)nums[i] <= t) || 
                (floor != null && (long)nums[i] - (long)floor <= t))
                return true;
            values.add(nums[i]);
        }
        return false;
    }
}

  

Approach #3 Python. [bucket]

class Solution(object):
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        if t < 0: return False
        n = len(nums)
        d = {}
        w = t + 1
        for i in xrange(n):
            m = nums[i] / w
            if m in d:
                return True
            if m-1 in d and abs(nums[i] - d[m-1]) < w:
                return True
            if m+1 in d and abs(nums[i] - d[m+1]) < w:
                return True
            d[m] = nums[i]
            if i >= k: del d[nums[i-k] / w]
        return False

  

Analysis:

In the case one we use brute froce to traverse the array. [Time Limit Exceded]

In the case two we use the mothod of floor and ceiling to find the element which in the range of index and satisfy the absolute difference between nums[i] and nums[j] is at most t.

In the case three we use the bucket to control the range of index.

TreeSet in Java (https://www.geeksforgeeks.org/treeset-in-java-with-examples/)

TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface. It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class.

永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/10023365.html