diff --git a/3sum/samthekorean.py b/3sum/samthekorean.py new file mode 100644 index 000000000..2bb94999f --- /dev/null +++ b/3sum/samthekorean.py @@ -0,0 +1,29 @@ +# O(n^2) where there is nested loop. +# O(1) where high and low are reused as constant values. + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + # Initialize a set to store unique triplets + triplets = set() + + # Sort the list to make two-pointer approach possible + nums.sort() + + # Iterate through the list, considering each element as a potential first element of a triplet + for i in range(len(nums) - 2): + low = i + 1 + high = len(nums) - 1 + + # To avoid duplicate iteration + while low < high: + three_sum = nums[i] + nums[low] + nums[high] + + if three_sum < 0: + low += 1 + elif three_sum > 0: + high -= 1 + else: + triplets.add((nums[i], nums[low], nums[high])) + low += 1 + high -= 1 diff --git a/encode-and-decode-strings/samthekorean.py b/encode-and-decode-strings/samthekorean.py new file mode 100644 index 000000000..501198109 --- /dev/null +++ b/encode-and-decode-strings/samthekorean.py @@ -0,0 +1,70 @@ +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + + # TC : O(n) where n is the combined length of the string in the list of strings. + # SC : O(S), where S is the sum of the lengths of all strings in strs + def encode(self, strs): + res = "" + for s in strs: + res += str(len(s)) + ":" + s + return res + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + + # TC : O(n) where n is the length of encoded string + # SC : O(S), where S is the total length of the decoded strings. + def decode(self, str): + res, i = [], 0 + + while i < len(str): + j = i + while str[j] != ":": + j += 1 + length = int(str[i:j]) + res.append(str[j + 1 : j + length + 1]) + i = j + 1 + length + + return res + + +def test_solution(): + solution = Solution() + + # Test case 1: Basic test case + input_strs = ["hello", "world"] + encoded = solution.encode(input_strs) + decoded = solution.decode(encoded) + print(f"Test case 1\nEncoded: {encoded}\nDecoded: {decoded}\n") + + # Test case 2: Empty list + input_strs = [] + encoded = solution.encode(input_strs) + decoded = solution.decode(encoded) + print(f"Test case 2\nEncoded: {encoded}\nDecoded: {decoded}\n") + + # Test case 3: List with empty strings + input_strs = ["", "", ""] + encoded = solution.encode(input_strs) + decoded = solution.decode(encoded) + print(f"Test case 3\nEncoded: {encoded}\nDecoded: {decoded}\n") + + # Test case 4: List with mixed empty and non-empty strings + input_strs = ["abc", "", "def"] + encoded = solution.encode(input_strs) + decoded = solution.decode(encoded) + print(f"Test case 4\nEncoded: {encoded}\nDecoded: {decoded}\n") + + # Test case 5: List with special characters + input_strs = ["he:llo", "wo:rld"] + encoded = solution.encode(input_strs) + decoded = solution.decode(encoded) + print(f"Test case 5\nEncoded: {encoded}\nDecoded: {decoded}\n") + + +test_solution() diff --git a/longest-consecutive-sequence/samthekorean.py b/longest-consecutive-sequence/samthekorean.py new file mode 100644 index 000000000..e416ed3fa --- /dev/null +++ b/longest-consecutive-sequence/samthekorean.py @@ -0,0 +1,15 @@ +# TC : O(n) where n is the length of nums +# SC : O(n) where n is the size of nums +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + numSet = set(nums) + longest = 0 + + for n in nums: + # check whether its the start of the sequence + if (n - 1) not in numSet: + length = 0 + while (n + length) in numSet: + length += 1 + longest = max(length, longest) + return longest diff --git a/product-of-array-except-self/samthekorean.py b/product-of-array-except-self/samthekorean.py new file mode 100644 index 000000000..a790d2d02 --- /dev/null +++ b/product-of-array-except-self/samthekorean.py @@ -0,0 +1,18 @@ +# TC : O(n) +# SC : O(1) + + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + ans = [1] * (len(nums)) + + prefix = 1 + for i in range(len(nums)): + ans[i] = prefix + prefix *= nums[i] + postfix = 1 + for i in range(len(nums) - 1, -1, -1): + ans[i] *= postfix + postfix *= nums[i] + + return ans diff --git a/top-k-frequent-elements/samthekorean.py b/top-k-frequent-elements/samthekorean.py new file mode 100644 index 000000000..9f8a685bc --- /dev/null +++ b/top-k-frequent-elements/samthekorean.py @@ -0,0 +1,31 @@ +# TC : O(n) +# SC : O(n) +from collections import defaultdict + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + # Initialize a list of empty lists to store numbers based on their frequencies + freq = [[] for i in range(len(nums) + 1)] + + # Dictionary to count the frequency of each number + counter = defaultdict(int) + + # Count the frequency of each number in nums + for num in nums: + counter[num] += 1 + + # Group numbers by their frequency + for n, c in counter.items(): + freq[c].append(n) + + # Result list to store the top k frequent elements + res = [] + + # Iterate over the frequency list in reverse order to get the most frequent elements first + for i in range(len(freq) - 1, 0, -1): + for n in freq[i]: + res.append(n) + # Once we have k elements in the result, return it + if len(res) == k: + return res