leetcode刷题笔记 220题 存在重复元素 III

leetcode刷题笔记 220题 存在重复元素 III

源地址:220. 存在重复元素 III

问题描述:

在整数数组 nums 中,是否存在两个下标 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值小于等于 t ,且满足 i 和 j 的差的绝对值也小于等于 ķ 。

如果存在则返回 true,不存在返回 false。

示例 1:

输入: nums = [1,2,3,1], k = 3, t = 0
输出: true
示例 2:

输入: nums = [1,0,1,1], k = 1, t = 2
输出: true
示例 3:

输入: nums = [1,5,9,1,5,9], k = 2, t = 3
输出: false

//使用TreeSet管理k个节点, TreeSet使用TreeMap构造,底层原理也是红黑树
import scala.collection.mutable
object Solution {
    def containsNearbyAlmostDuplicate(nums: Array[Int], k: Int, t: Int): Boolean = {
        val set = new mutable.TreeSet[Long]()
        
        for (i <- 0 to nums.length-1) {
            val maxBefore = set.maxBefore(nums(i).toLong)
            if (maxBefore != None && maxBefore.get + t >= nums(i)) return true
            
            val minAfter = set.minAfter(nums(i).toLong)
            if (minAfter != None && minAfter.get <= nums(i) + t) return true
            
            set.add(nums(i))
            if (set.size > k) set.remove(nums(i-k))
        }
        
        return false
    }
}

//以t长度进行分桶,每个数所在的桶及相邻的桶中判断是否满足条件
//维护K个节点的映射关系
import scala.collection.mutable
object Solution {
    def containsNearbyAlmostDuplicate(nums: Array[Int], k: Int, t: Int): Boolean = {
        if (k <= 0 || t < 0) return false
        
        def getId(i: Int): Int = {
            if (i >= 0) return i / (t+1)
            else return i / (t+1) - 1
        }
        
        val bucket = mutable.HashMap[Int, Int]()
        
        for (i <- 0 to nums.length-1) {
            val elem = nums(i)
            val bucketId = getId(elem)
            
            //位于桶内或者相邻桶
            if (bucket.contains(bucketId)) return true
            if (bucket.contains(bucketId-1) && (elem.toLong - bucket(bucketId -1).toLong <= t)) return true
            if (bucket.contains(bucketId+1) && (bucket(bucketId+1).toLong - elem.toLong <= t)) return true
            
            //控制桶内个数
            if (i >= k) {
                val last = getId(nums(i - k))
                bucket.remove(last)
            }
            bucket.put(bucketId, elem)
        }
        
        return false
    }
}
原文地址:https://www.cnblogs.com/ganshuoos/p/13774018.html