Skip to content

Commit 27e3ae2

Browse files
committed
217_Contains_Duplicate
219_Contains_Duplicate_II 220_Contains_Duplicate_III
1 parent 1fd00ae commit 27e3ae2

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@
6666
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/166_Fraction_to_Recurring_Decimal.py) | % and Hash to find duplicate |
6767
| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/167_Two_Sum_II_Input_array_is_sorted.py) | Two points O(n) and O(1) |
6868
| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) ♥| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
69+
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/217_Contains_Duplicate.py) | 1. Set and compare length<br>2. Sort and check i,i +1|
70+
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/219_Contains_Duplicate_II.py) | 1. Brute force<br> 2. Maintenance a set that contains previous k numbers, and check if curr in set |
71+
| 220 | [Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/220_Contains_Duplicate_III.py) | 1. Sort and binary Search <br>2. Bucket sort |
6972
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|

python/217_Contains_Duplicate.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ def containsDuplicate(self, nums):
55
:rtype: bool
66
"""
77
# use set to check duplicate
8-
return len(nums) != len(set(nums))
8+
return len(nums) != len(set(nums))
9+
10+
# def containsDuplicate(self, nums):
11+
# nums.sort()
12+
# for i in range(len(nums) - 1):
13+
# if nums[i] == nums[i + 1]:
14+
# return True
15+
# return False

python/220_Contains_Duplicate_III.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import OrderedDict
12
class Solution(object):
23
def containsNearbyAlmostDuplicate(self, nums, k, t):
34
"""
@@ -6,3 +7,22 @@ def containsNearbyAlmostDuplicate(self, nums, k, t):
67
:type t: int
78
:rtype: bool
89
"""
10+
# https://discuss.leetcode.com/topic/19991/o-n-python-using-buckets-with-explanation-10-lines
11+
# Bucket sort. Each bucket has size of t. For each number, the possible
12+
# candidate can only be in the same bucket or the two buckets besides.
13+
# Keep as many as k buckets to ensure that the difference is at most k.
14+
buckets = {}
15+
for i, v in enumerate(nums):
16+
# t == 0 is a special case where we only have to check the bucket
17+
# that v is in.
18+
bucketNum, offset = (v / t, 1) if t else (v, 0)
19+
for idx in xrange(bucketNum - offset, bucketNum + offset + 1):
20+
if idx in buckets and abs(buckets[idx] - nums[i]) <= t:
21+
return True
22+
23+
buckets[bucketNum] = nums[i]
24+
if len(buckets) > k:
25+
# Remove the bucket which is too far away. Beware of zero t.
26+
del buckets[nums[i - k] / t if t else nums[i - k]]
27+
28+
return False

0 commit comments

Comments
 (0)