From fe46566622535eac08c1d4980780fac9e52bf3c5 Mon Sep 17 00:00:00 2001 From: Phuc Le Date: Sun, 24 Nov 2019 13:11:44 -0800 Subject: [PATCH 1/2] Add Solution.py for problems 0219 --- .../0219.Contains Duplicate II/Solution.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 solution/0219.Contains Duplicate II/Solution.py diff --git a/solution/0219.Contains Duplicate II/Solution.py b/solution/0219.Contains Duplicate II/Solution.py new file mode 100644 index 0000000000000..3552e9df321fc --- /dev/null +++ b/solution/0219.Contains Duplicate II/Solution.py @@ -0,0 +1,25 @@ +''' +https://leetcode.com/problems/contains-duplicate-ii/ + +Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. +''' +''' +Runtime: 96 ms, faster than 93.53% of Python3 online submissions for Contains Duplicate II. +Memory Usage: 20.5 MB, less than 62.50% of Python3 online submissions for Contains Duplicate II. +''' + +# Create a hashmap to remember the most recent position of unique values +# If we found duplicate and the range is less than k, then return true +# Else remember that index +class Solution: + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: + pos = {} + for idx, element in enumerate(nums): + if element in pos and idx - pos[element] <= k: + return True + pos[element] = idx + return False + + + + \ No newline at end of file From 167e71368b88a7c35a434077a27c433697f94b64 Mon Sep 17 00:00:00 2001 From: Phuc Le Date: Sun, 24 Nov 2019 13:14:07 -0800 Subject: [PATCH 2/2] Add comments for problem 0219 --- solution/0219.Contains Duplicate II/Solution.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/solution/0219.Contains Duplicate II/Solution.py b/solution/0219.Contains Duplicate II/Solution.py index 3552e9df321fc..023d08d5f66be 100644 --- a/solution/0219.Contains Duplicate II/Solution.py +++ b/solution/0219.Contains Duplicate II/Solution.py @@ -3,14 +3,21 @@ Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. ''' +# Performance ''' Runtime: 96 ms, faster than 93.53% of Python3 online submissions for Contains Duplicate II. Memory Usage: 20.5 MB, less than 62.50% of Python3 online submissions for Contains Duplicate II. ''' -# Create a hashmap to remember the most recent position of unique values -# If we found duplicate and the range is less than k, then return true -# Else remember that index +# Algorithm Explained +''' +Create a hashmap to remember the most recent position of unique values +If we found duplicate and the range is less than k, then return true +Else remember that index + +Space: O(n) with n is the number of original array +Complexity: O(n) +''' class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: pos = {}