From 90fb6db0fb9ad63405627d2227965680ce872766 Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Sun, 21 Jun 2026 19:15:59 +0900 Subject: [PATCH 1/7] 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/7] 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/7] 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/7] 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 [] From 97883c1daca7efb7cd77badfa9f7046aeb800cbb Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Wed, 24 Jun 2026 23:42:18 +0900 Subject: [PATCH 5/7] top k frequent elements solution --- top-k-frequent-elements/dolphinflow86.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 top-k-frequent-elements/dolphinflow86.py diff --git a/top-k-frequent-elements/dolphinflow86.py b/top-k-frequent-elements/dolphinflow86.py new file mode 100644 index 0000000000..6c8494b637 --- /dev/null +++ b/top-k-frequent-elements/dolphinflow86.py @@ -0,0 +1,22 @@ +# 1) Using modified bucket sort approach. +# TC: O(N) where N is the length of nums +# SC: O(N) where N is the length of nums +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + freq_map = {} + + bucket_count = len(nums) + for num in nums: + freq_map[num] = freq_map.get(num, 0) + 1 + + buckets = [[] for i in range(bucket_count + 1)] + + for num, freq in freq_map.items(): + buckets[freq].append(num) + + top_list = [] + for i in range(bucket_count, -1, -1): + for num in buckets[i]: + top_list.append(num) + if len(top_list) == k: + return top_list From 0d1e9d80462bc1e1b5a235d0633660d1c33676b7 Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Fri, 26 Jun 2026 10:41:32 +0900 Subject: [PATCH 6/7] longest consecutive sequence solution --- longest-consecutive-sequence/dolphinflow86.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 longest-consecutive-sequence/dolphinflow86.py diff --git a/longest-consecutive-sequence/dolphinflow86.py b/longest-consecutive-sequence/dolphinflow86.py new file mode 100644 index 0000000000..2ef2d43127 --- /dev/null +++ b/longest-consecutive-sequence/dolphinflow86.py @@ -0,0 +1,26 @@ +# Calculate the length of the streak from its starting point. +# TC: O(N) where N is the size of nums +# SC: O(N) where N is the size of nums +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + # convert list to set for O(1) lookups + num_set: Set[int] = set(nums) + max_streak = 0 + + for num in num_set: + if num - 1 in num_set: + continue + + # found streak starting point + streak = 0 + cur_num = num + + # extend the streak as long as consecutive numbers exists + while cur_num in num_set: + streak += 1 + cur_num += 1 + + # update max streak + max_streak = max(streak, max_streak) + + return max_streak From d3588841ed4ff699421793de86ef6f64c226670f Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Fri, 26 Jun 2026 21:21:17 +0900 Subject: [PATCH 7/7] house robber solution --- house-robber/dolphinflow86.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 house-robber/dolphinflow86.py diff --git a/house-robber/dolphinflow86.py b/house-robber/dolphinflow86.py new file mode 100644 index 0000000000..3dea4f6a12 --- /dev/null +++ b/house-robber/dolphinflow86.py @@ -0,0 +1,17 @@ +# Solved using a top down dp (memoization) approach. +# TC: O(N) where N = len(nums) - Each house is visited at most once due to caching. +# SC: O(N) where N = len(nums) - Used for the memoization dictionary and recursion call stack. +class Solution: + def rec(self, house: int, nums: List[int], memo: Dict[int,int]) -> int: + if house >= len(nums): return 0 + + if house in memo: return memo[house] + + # max between (robbing current house + skipping next) or (skipping current house) + current_robbed = max(nums[house] + self.rec(house + 2, nums, memo), self.rec(house + 1, nums, memo)) + memo[house] = current_robbed + return current_robbed + + def rob(self, nums: List[int]) -> int: + memo: Dict[int,int] = {} + return self.rec(0, nums, memo)