diff --git a/3sum/bhyun-kim.py b/3sum/bhyun-kim.py new file mode 100644 index 000000000..f92281660 --- /dev/null +++ b/3sum/bhyun-kim.py @@ -0,0 +1,83 @@ +""" +15. 3Sum +https://leetcode.com/problems/3sum/description/ + +Solution: + - Sort the list + - Iterate through the list + - For each element, find the two elements that sum up to -element + - Use two pointers to find the two elements + - Skip the element if it is the same as the previous element + - Skip the two elements if they are the same as the previous two elements + - Add the set to the output list + + Example: + ----------------------------------------- + low : | + high: | + i : | + [-4,-1,-1,0,1,2] + + ----------------------------------------- + low : | + high: | + i : | + [-4,-1,-1,0,1,2] + + ... no possible set with i=-4, and high=2 + + ----------------------------------------- + low : | + high: | + i : | + [-4,-1,-1,0,1,2] + + +Time complexity: O(n^2) + - O(nlogn) for sorting + - O(n^2) for iterating through the list and finding the two elements + - Total: O(n^2) +Space complexity: O(n) + - O(n) for the output list + - O(n) for the prevs dictionary + - O(n) for the prevs_n set + - Total: O(n) +""" + + +from typing import List + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums = sorted(nums) + output = [] + prevs = dict() + prevs_n = set() + + for i in range(len(nums) - 2): + n_i = nums[i] + + if n_i in prevs_n: + continue + else: + prevs_n.add(n_i) + + low, high = i + 1, len(nums) - 1 + while low < high: + n_low = nums[low] + n_high = nums[high] + if n_i + n_low + n_high == 0: + if not f"[{n_i},{n_low},{n_high}]" in prevs: + prevs[f"[{n_i},{n_low},{n_high}]"] = 1 + output.append([n_i, n_low, n_high]) + low += 1 + high -= 1 + + elif n_i + n_low + n_high < 0: + low += 1 + + elif n_i + n_low + n_high > 0: + high -= 1 + + return output diff --git a/encode-and-decode-strings/bhyun-kim.py b/encode-and-decode-strings/bhyun-kim.py new file mode 100644 index 000000000..bdc35b0ab --- /dev/null +++ b/encode-and-decode-strings/bhyun-kim.py @@ -0,0 +1,34 @@ +""" +271. Encode and Decode Strings +https://leetcode.com/problems/encode-and-decode-strings/description/ + +Solution: + - Concatenate the strings with a special character + - Split the string by the special character + +Time complexity: O(n) + - Concatenating the strings: O(n) + - Splitting the string: O(n) + - Total: O(n) + +Space complexity: O(n) + - Storing the output: O(n) + - Total: O(n) +""" +from typing import List + + +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string.""" + output = strs[0] + + for i in range(1, len(strs)): + output += "é" + strs[i] + + return output + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings.""" + + return s.split("é") diff --git a/longest-consecutive-sequence/bhyun-kim.py b/longest-consecutive-sequence/bhyun-kim.py new file mode 100644 index 000000000..460e5a4aa --- /dev/null +++ b/longest-consecutive-sequence/bhyun-kim.py @@ -0,0 +1,40 @@ +""" +128. Longest Consecutive Sequence +https://leetcode.com/problems/longest-consecutive-sequence/description/ + +Solution: + - Create a set of the input list + - Iterate through the set + - For each element, find the consecutive elements + - Use a while loop to find the consecutive elements + - Update the longest length + +Time complexity: O(n) + - O(n) for iterating through the set + +Space complexity: O(n) + - O(n) for the set + - Total: O(n) + +""" + +from typing import List + + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + longest_len = 0 + nums_set = set(nums) + + for n in nums_set: + if n - 1 not in nums_set: + current_n = n + current_len = 1 + + while current_n + 1 in nums_set: + current_n += 1 + current_len += 1 + + longest_len = max(longest_len, current_len) + + return longest_len diff --git a/product-of-array-except-self/bhyun-kim.py b/product-of-array-except-self/bhyun-kim.py new file mode 100644 index 000000000..c46d07ad1 --- /dev/null +++ b/product-of-array-except-self/bhyun-kim.py @@ -0,0 +1,65 @@ +""" +238. Product of Array Except Self +https://leetcode.com/problems/product-of-array-except-self/description/ + +Solution: + - Create two lists to store the product of elements from the left and right + - Multiply the elements from the left and right lists to get the output + + Example: + nums = [4, 2, 3, 1, 7, 0, 9, 10] + + left is numbers to the left of the current index + right is numbers to the right of the current index + from_left is the product of the numbers to the left of the current index + from_right is the product of the numbers to the right of the current index + + i = 0: (4) 2 3 1 7 0 9 10 + i = 1: 4 (2) 3 1 7 0 9 10 + i = 2: 4 2 (3) 1 7 0 9 10 + i = 3: 4 2 3 (1) 7 0 9 10 + i = 4: 4 2 3 1 (7) 0 9 10 + i = 5: 4 2 3 1 7 (0) 9 10 + i = 6: 4 2 3 1 7 0 (9) 10 + i = 7: 4 2 3 1 7 0 9 (10) + + from_left = [0, 4, 8, 24, 24, 168, 0, 0] + from_right = [0, 0, 0, 0, 0, 90, 10, 0] + output = [0, 0, 0, 0, 0, 15120, 0, 0] + +Time complexity: O(n) + - Calculating the product of the elements from the left: O(n) + - Calculating the product of the elements from the right: O(n) + - Calculating the output: O(n) + - Total: O(n) + +Space complexity: O(n) + - Storing the product of the elements from the left: O(n) + - Storing the product of the elements from the right: O(n) + - Storing the output: O(n) +""" +from typing import List + + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + output = [0] * len(nums) + + from_left = [0] * (len(nums)) + from_right = [0] * (len(nums)) + + from_left[0] = nums[0] + from_right[-1] = nums[-1] + + for i in range(1, len(nums) - 1): + from_left[i] = from_left[i - 1] * nums[i] + + for i in reversed(range(1, len(nums) - 1)): + from_right[i] = from_right[i + 1] * nums[i] + + output[0] = from_right[1] + output[-1] = from_left[-2] + for i in range(1, len(nums) - 1): + output[i] = from_left[i - 1] * from_right[i + 1] + + return output diff --git a/top-k-frequent-elements/bhyun-kim.py b/top-k-frequent-elements/bhyun-kim.py new file mode 100644 index 000000000..d4694d2ce --- /dev/null +++ b/top-k-frequent-elements/bhyun-kim.py @@ -0,0 +1,43 @@ +""" +347. Top K Frequent Elements +https://leetcode.com/problems/top-k-frequent-elements/description/ + +Solution: + - Count the frequency of each element in the list + - Sort the dictionary by the frequency in descending order + - Return the first k elements in the sorted dictionary + +Time complexity: O(nlogn) + - Counting the frequency of each element: O(n) + - Sorting the dictionary: O(nlogn) + - Returning the first k elements: O(k) + k <= n + - Total: O(nlogn) + +Space complexity: O(n) + - Storing the frequency of each element: O(n) + - Storing the sorted dictionary: O(n) + - Storing the output: O(k) + k <= n +""" +from collections import OrderedDict +from typing import List + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + n_dict = OrderedDict() + + for n in nums: + if n in n_dict: + n_dict[n] += 1 + else: + n_dict[n] = 1 + + n_dict = sorted(n_dict.items(), key=lambda x: x[1], reverse=True) + + output = [0] * k + for i in range(k): + output[i] = n_dict[i][0] + + return output