diff --git a/3sum/dolphinflow86.py b/3sum/dolphinflow86.py new file mode 100644 index 0000000000..44de609f17 --- /dev/null +++ b/3sum/dolphinflow86.py @@ -0,0 +1,21 @@ +# 1) Fix one element first, and then treat others as two-sum problem. Use set to get rid of duplicate combination. +# TC: O(N^2) where N is the length of nums. +# SC: O(N) where N is the length of nums. +class Solution: + def threeSum(self, nums: list[int]) -> list[list[int]]: + answer: Set[tuple[int,int,int]] = set() + n = len(nums) + nums.sort() + + for i in range(n-2): + if i > 0 and nums[i] == nums[i-1]: continue + + target = -nums[i] + nums_map: Dict[int, int] = {} + for j in range(i + 1, n): + comp = target - nums[j] + if comp in nums_map: + answer.add((nums[i], comp, nums[j])) + nums_map[nums[j]] = j + + return [list(triplet) for triplet in answer] diff --git a/climbing-stairs/dolphinflow86.py b/climbing-stairs/dolphinflow86.py new file mode 100644 index 0000000000..8449501344 --- /dev/null +++ b/climbing-stairs/dolphinflow86.py @@ -0,0 +1,20 @@ +# 1) Recursion with memoization. +# TC: O(N) where N is the given natural number. +# SC: O(N) where N is the given natural number. +class Solution: + def climb_rec(self, n: int, step: int, memo: Dict[int, int]): + if step > n: return 0 + if step in memo: return memo[step] + if step == n: return 1 + + first_way = self.climb_rec(n, step + 1, memo) + second_way = self.climb_rec(n, step + 2, memo) + memo[step] = first_way + second_way + return memo[step] + + + def climbStairs(self, n: int) -> int: + if n == 1: return 1 + + memo = {} + return self.climb_rec(n, 1, memo) + self.climb_rec(n, 2, memo) diff --git a/product-of-array-except-self/dolphinflow86.py b/product-of-array-except-self/dolphinflow86.py new file mode 100644 index 0000000000..79455610a6 --- /dev/null +++ b/product-of-array-except-self/dolphinflow86.py @@ -0,0 +1,20 @@ +# 1) Without using division operator and need to meet linear time complexity, +# calculate left accumulated product and calculate right accumulated product except iself and then +# product of left and right to get the result. +# TC: O(N) where N is the length of nums +# SC: O(N) where N is the length of nums +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + answer = [1] * n + acc = 1 + for i in range(1, n): + acc *= nums[i-1] + answer[i] = acc + + acc = 1 + for i in range(n-2, -1, -1): + acc *= nums[i+1] + answer[i] *= acc + + return answer diff --git a/valid-anagram/dolphinflow86.py b/valid-anagram/dolphinflow86.py new file mode 100644 index 0000000000..fbecb61bd4 --- /dev/null +++ b/valid-anagram/dolphinflow86.py @@ -0,0 +1,25 @@ +# 1) Sort input strings and check if those are the same. +# TC: O(NlogN) where N is the size of string s and t due to sorting +# SC: O(N) where N is the length of string s and t to store sorted list. +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return sorted(s) == sorted(t) + +# 2) Using dict to store count of the first string and decrease back to see if there's negative count. +# TC: O(N + M) where N is the length of s and M is the length of t. +# SC: O(N + M) where N is the length of s and M is the length of t. +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): return False + + char_map: Dict[str, int] = {} + for ch in s: + char_map[ch] = char_map.get(ch, 0) + 1 + + for ch in t: + if ch not in char_map or char_map[ch] == 0: + return False + + char_map[ch] -= 1 + + return True