Contains Duplicate III -leetcode

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 difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.

  关于上述题意,我们可以转化为两个数学公式:

  存在|nums[i]-nums[j]|<=t且|i-j|<=k,为了计算方便,首先需要对前者进行变形。

  |nums[i]-nums[j]|<=t    ->       |nums[i]/t-nums[j]/t|<=1    ->       nums[j]/t - 1<= nums[i]/t <= nums[j]+1

则当nums[i]/t和nums[j]/t都同时取整,所以如果nums[i]/t能够满足上式,则取值只能为(nums[j]/t-1, nums[j], nums[j]+1)

  


 1 from collections import OrderedDict
 2
 3
 4 class Solution:
 5     def containsNearbyAlmostDuplicate(self, nums, k, t):
 6         orderedDic = OrderedDict()
 7
 8         if k < 1 or t < 0:
 9             return False
10
11         for each in nums:
12             key = each/max(1, t)
13
14             for m in (key-1, key, key+1):
15                 if m in orderedDic and abs(orderedDic[m] - each) <= t:
16                     return True
17
18             orderedDic[key] = each
19
20             if len(orderedDic) > k:
21                 orderedDic.popitem(last=False)
22
23         return False


  接下来,一行一行解释代码的意思。

  第1行,from collections import OrderedDict。这里主要是需要使用OrderedDict.OrderedDict与一般的Dict最大的区别在于,它是有序的。

  第8行,这里需要对没有意义的数据进行处理;

  第12行,根据上面的推理,我们字典的键值定义为each/max(1,t),这里为什么是max(1,t),而不直接是max(t),这里主要是考虑t=0的情况;

  第14-21行,对nums每个元素进行遍历,如果每一个元素m,将m/t作为键值,如果在orderedDic中,存在一个键值属于(m/t-1,m/t,m/t+1),则这就满足了上面推到的数学公式,但需要注意上面的数学公式并不是充要条件,所以需要判断abs(orderedDic[m] - each) <= t,当此时m/t不在orderedDic中,且orderedDic的长度<=k,则直接将此键值加入字典中,但是orderedDic的长度大于k,这时需要将最先加入的键值弹出,这也是使用orderedDic的原因。

  可能有些同志存在疑问,为什么当字典长度大于k时,需要将多余的键值弹出,而且还要弹出第一个存入的键值?

  |i-j|<=k这个条件就决定了,字典长度最大只能为k。假如,字典的长度为k+1,此时第k+2个元素与第一个元素满足|nums[0]-nums[k+1]|<=t条件,但它们不会满足|i-j|<=k,  因为|k+1-0|>=k。

时间: 2024-12-31 07:24:11

Contains Duplicate III -leetcode的相关文章

Contains Duplicate III —— LeetCode

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 题目大意:给定一个数组,找出是否存在两个不同下标的值相差<=t,下标i和j相

[LeetCode][Java]Contains Duplicate III

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 difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 数组中是否存在两个元素,他们的

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

Contains Duplicate,Contains Duplicate II,Contains Duplicate III

217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 判断数组是是否含有重复元素.

Best Time to Buy and Sell Stock III leetcode java

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time

[LeetCode] 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 difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 参考资料: https://leetcode.com/discuss/381

leetcode笔记: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 difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 二. 题目分析 题目大意是,给定一个整数数组,判断其中是否存

Leetcode 220 Contains Duplicate III

1. 问题描述 给定一个整数数组nums[],查找是否存在两个下标i和j,满足|numsi?numsj|≤t 且 |i?j|≤k. 2. 方法与思路 总得思路就是:"滑动窗口"+unordered_map. 推理过程如下: |numsi?numsj|≤t?|numsi/t?numsj/t|≤1: 由上式可以推出:|?numsi/t???numsj/t?|≤1 等价于:?numsi/t?∈{?numsj/t?,?numsj/t??1,?numsj/t?+1}. 我们只需要维持一个大小为K

Leetcode: 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 difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可