From 90fb6db0fb9ad63405627d2227965680ce872766 Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Sun, 21 Jun 2026 19:15:59 +0900 Subject: [PATCH 1/4] contains duplicate solutions --- contains-duplicate/dolphinflow86.py | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 contains-duplicate/dolphinflow86.py diff --git a/contains-duplicate/dolphinflow86.py b/contains-duplicate/dolphinflow86.py new file mode 100644 index 0000000000..f1c0d91583 --- /dev/null +++ b/contains-duplicate/dolphinflow86.py @@ -0,0 +1,44 @@ +# First approach: using a set to check for duplicates while iterating through nums +# TC: O(n), where n is the number of elements in nums +# SC: O(n), where n is the number of elements in nums +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + seen = set() + + for num in nums: + # check if the number exists in the set during each iteration + if num in seen: + return True + seen.add(num) + + return False + + +# Second approach: brute-force method using nested for loops to check every pair for duplicates. +# Inefficient time complexity (quadratic time) but has a constant space complexity. +# TC: O(n^2), where n is the number of elements in nums +# SC: O(1) +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] == nums[j]: + return True + + return False + + +# Third approach: to decrease time complexity from the brute-force approach, by sorting the array and comparing adjacent elements +# TC: O(n log n), where n is the number of elements in nums +# SC: O(n), where n is the number of elements in nums. Initially I thought Python's sorting algorithm was the same as C++'s IntroSort. +# However, Python uses Timsort uses which requires O(n) space in the worst case +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + nums.sort() + + # Compare adjacent elements + for i in range(len(nums) - 1): + if nums[i] == nums[i + 1]: + return True + + return False \ No newline at end of file From 49f79f65c847105a543da653039fe432028d276d Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Sun, 21 Jun 2026 19:34:31 +0900 Subject: [PATCH 2/4] add missing empty line --- contains-duplicate/dolphinflow86.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contains-duplicate/dolphinflow86.py b/contains-duplicate/dolphinflow86.py index f1c0d91583..638926db7d 100644 --- a/contains-duplicate/dolphinflow86.py +++ b/contains-duplicate/dolphinflow86.py @@ -41,4 +41,5 @@ def containsDuplicate(self, nums: List[int]) -> bool: if nums[i] == nums[i + 1]: return True - return False \ No newline at end of file + return False + \ No newline at end of file From a750baccc4c9d7ddabe480b319a409721a7ef626 Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Sun, 21 Jun 2026 19:51:01 +0900 Subject: [PATCH 3/4] remove empty space --- contains-duplicate/dolphinflow86.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contains-duplicate/dolphinflow86.py b/contains-duplicate/dolphinflow86.py index 638926db7d..c627f241c2 100644 --- a/contains-duplicate/dolphinflow86.py +++ b/contains-duplicate/dolphinflow86.py @@ -42,4 +42,3 @@ def containsDuplicate(self, nums: List[int]) -> bool: return True return False - \ No newline at end of file From e7845589f6462fdd03da9aa16a4d160496e2c5f9 Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Mon, 22 Jun 2026 22:18:57 +0900 Subject: [PATCH 4/4] two sum solutions --- two-sum/dolphinflow86.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 two-sum/dolphinflow86.py diff --git a/two-sum/dolphinflow86.py b/two-sum/dolphinflow86.py new file mode 100644 index 0000000000..4c863b6ff3 --- /dev/null +++ b/two-sum/dolphinflow86.py @@ -0,0 +1,44 @@ +# 1) Using nested for loop to find every possible combination for target sum +# TC: O(n^2) where n is the size of nums +# SC: O(1) +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] + nums[j] == target: + return [i , j] + + return [] + +#2) Using two pass approach with hash map so that find out complement with index. +# TC: O(2*n) -> O(n) where n is the size of nums +# SC: O(n) where n is the size of nums +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + seen: dict[int, int] = {} + + for i in range(len(nums)): + seen[nums[i]] = i + + for i in range(len(nums)): + complement = target - nums[i] + if complement in seen and i != seen[complement]: + return [seen[complement], i] + + return [] + +# 3) Tiny optimize from two pass version. Instead of inserting separately, insert within one loop +# TC: O(n), where n is the size of nums +# SC: O(n), where n is the size of nums +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + seen: dict[int, int] = {} + + for i in range(len(nums)): + complement = target - nums[i] + if complement in seen: + return [seen[complement], i] + + seen[nums[i]] = i + + return []