{"id":4515,"date":"2018-12-20T21:55:24","date_gmt":"2018-12-21T05:55:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4515"},"modified":"2018-12-20T21:55:34","modified_gmt":"2018-12-21T05:55:34","slug":"leetcode-220-contains-duplicate-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-220-contains-duplicate-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 220. Contains Duplicate III"},"content":{"rendered":"\n<p>Given an array of integers, find out whether there are two distinct indices&nbsp;<em>i<\/em>&nbsp;and&nbsp;<em>j<\/em>&nbsp;in the array such that the&nbsp;<strong>absolute<\/strong>&nbsp;difference between&nbsp;<strong>nums[i]<\/strong>&nbsp;and&nbsp;<strong>nums[j]<\/strong>&nbsp;is at most&nbsp;<em>t<\/em>&nbsp;and the&nbsp;<strong>absolute<\/strong>&nbsp;difference between&nbsp;<em>i<\/em>&nbsp;and&nbsp;<em>j<\/em>&nbsp;is at most&nbsp;<em>k<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>nums = [1,2,3,1], k = 3, t = 0<br><strong>Output: <\/strong>true<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>nums = [1,0,1,1], k = 1, t = 2<br><strong>Output: <\/strong>true<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>nums = [1,5,9,1,5,9], k = 2, t = 3<br><strong>Output: <\/strong>false<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution: Sliding Window + Multiset (OrderedSet)<\/strong><\/h1>\n\n\n\n<p>Maintaining a sliding window of sorted numbers of k + 1. After the i-th number was inserted into the sliding window, check whether its left and right neighbors satisfy abs(nums[i] &#8211; neighbor) &lt;= t<\/p>\n\n\n\n<p>Time complexity: O(nlogk)<br>Space complexity: O(k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 12 ms\nclass Solution {\npublic:\n  bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n    multiset<long> s;\n    for (int i = 0; i < nums.size(); ++i) {\n      if (i > k)\n        s.erase(s.find(nums[i - k - 1]));\n            \n      auto it = s.insert(nums[i]);\n      if (it != begin(s) && *it - *prev(it) <= t)\n        return true;\n      if (next(it) != end(s) &#038;&#038; *next(it) - *it <= t)\n        return true;\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers, find out whether there are two distinct indices&nbsp;i&nbsp;and&nbsp;j&nbsp;in the array such that the&nbsp;absolute&nbsp;difference between&nbsp;nums[i]&nbsp;and&nbsp;nums[j]&nbsp;is at most&nbsp;t&nbsp;and the&nbsp;absolute&nbsp;difference between&nbsp;i&nbsp;and&nbsp;j&nbsp;is at most&nbsp;k.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[177,335,215,185],"class_list":["post-4515","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-medium","tag-multiset","tag-sliding-window","tag-sorted","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4515"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4515\/revisions"}],"predecessor-version":[{"id":4517,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4515\/revisions\/4517"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}