Skip to content

Commit 67333cf

Browse files
committed
[bhyun-kim, 김병현] Week 5 Solutions
1 parent 0fe554a commit 67333cf

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

3sum/bhyun-kim.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
15. 3Sum
3+
https://leetcode.com/problems/3sum/description/
4+
5+
Solution:
6+
- Sort the list
7+
- Iterate through the list
8+
- For each element, find the two elements that sum up to -element
9+
- Use two pointers to find the two elements
10+
- Skip the element if it is the same as the previous element
11+
- Skip the two elements if they are the same as the previous two elements
12+
- Add the set to the output list
13+
14+
Example:
15+
-----------------------------------------
16+
low : |
17+
high: |
18+
i : |
19+
[-4,-1,-1,0,1,2]
20+
21+
-----------------------------------------
22+
low : |
23+
high: |
24+
i : |
25+
[-4,-1,-1,0,1,2]
26+
27+
... no possible set with i=-4, and high=2
28+
29+
-----------------------------------------
30+
low : |
31+
high: |
32+
i : |
33+
[-4,-1,-1,0,1,2]
34+
35+
36+
Time complexity: O(n^2)
37+
- O(nlogn) for sorting
38+
- O(n^2) for iterating through the list and finding the two elements
39+
- Total: O(n^2)
40+
Space complexity: O(n)
41+
- O(n) for the output list
42+
- O(n) for the prevs dictionary
43+
- O(n) for the prevs_n set
44+
- Total: O(n)
45+
"""
46+
47+
48+
from typing import List
49+
50+
51+
class Solution:
52+
def threeSum(self, nums: List[int]) -> List[List[int]]:
53+
nums = sorted(nums)
54+
output = []
55+
prevs = dict()
56+
prevs_n = set()
57+
58+
for i in range(len(nums) - 2):
59+
n_i = nums[i]
60+
61+
if n_i in prevs_n:
62+
continue
63+
else:
64+
prevs_n.add(n_i)
65+
66+
low, high = i + 1, len(nums) - 1
67+
while low < high:
68+
n_low = nums[low]
69+
n_high = nums[high]
70+
if n_i + n_low + n_high == 0:
71+
if not f"[{n_i},{n_low},{n_high}]" in prevs:
72+
prevs[f"[{n_i},{n_low},{n_high}]"] = 1
73+
output.append([n_i, n_low, n_high])
74+
low += 1
75+
high -= 1
76+
77+
elif n_i + n_low + n_high < 0:
78+
low += 1
79+
80+
elif n_i + n_low + n_high > 0:
81+
high -= 1
82+
83+
return output
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
271. Encode and Decode Strings
3+
https://leetcode.com/problems/encode-and-decode-strings/description/
4+
5+
Solution:
6+
- Concatenate the strings with a special character
7+
- Split the string by the special character
8+
9+
Time complexity: O(n)
10+
- Concatenating the strings: O(n)
11+
- Splitting the string: O(n)
12+
- Total: O(n)
13+
14+
Space complexity: O(n)
15+
- Storing the output: O(n)
16+
- Total: O(n)
17+
"""
18+
from typing import List
19+
20+
21+
class Codec:
22+
def encode(self, strs: List[str]) -> str:
23+
"""Encodes a list of strings to a single string."""
24+
output = strs[0]
25+
26+
for i in range(1, len(strs)):
27+
output += "é" + strs[i]
28+
29+
return output
30+
31+
def decode(self, s: str) -> List[str]:
32+
"""Decodes a single string to a list of strings."""
33+
34+
return s.split("é")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
128. Longest Consecutive Sequence
3+
https://leetcode.com/problems/longest-consecutive-sequence/description/
4+
5+
Solution:
6+
- Create a set of the input list
7+
- Iterate through the set
8+
- For each element, find the consecutive elements
9+
- Use a while loop to find the consecutive elements
10+
- Update the longest length
11+
12+
Time complexity: O(n)
13+
- O(n) for iterating through the set
14+
15+
Space complexity: O(n)
16+
- O(n) for the set
17+
- Total: O(n)
18+
19+
"""
20+
21+
from typing import List
22+
23+
24+
class Solution:
25+
def longestConsecutive(self, nums: List[int]) -> int:
26+
longest_len = 0
27+
nums_set = set(nums)
28+
29+
for n in nums_set:
30+
if n - 1 not in nums_set:
31+
current_n = n
32+
current_len = 1
33+
34+
while current_n + 1 in nums_set:
35+
current_n += 1
36+
current_len += 1
37+
38+
longest_len = max(longest_len, current_len)
39+
40+
return longest_len
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
238. Product of Array Except Self
3+
https://leetcode.com/problems/product-of-array-except-self/description/
4+
5+
Solution:
6+
- Create two lists to store the product of elements from the left and right
7+
- Multiply the elements from the left and right lists to get the output
8+
9+
Example:
10+
nums = [4, 2, 3, 1, 7, 0, 9, 10]
11+
12+
left is numbers to the left of the current index
13+
right is numbers to the right of the current index
14+
from_left is the product of the numbers to the left of the current index
15+
from_right is the product of the numbers to the right of the current index
16+
17+
i = 0: (4) 2 3 1 7 0 9 10
18+
i = 1: 4 (2) 3 1 7 0 9 10
19+
i = 2: 4 2 (3) 1 7 0 9 10
20+
i = 3: 4 2 3 (1) 7 0 9 10
21+
i = 4: 4 2 3 1 (7) 0 9 10
22+
i = 5: 4 2 3 1 7 (0) 9 10
23+
i = 6: 4 2 3 1 7 0 (9) 10
24+
i = 7: 4 2 3 1 7 0 9 (10)
25+
26+
from_left = [0, 4, 8, 24, 24, 168, 0, 0]
27+
from_right = [0, 0, 0, 0, 0, 90, 10, 0]
28+
output = [0, 0, 0, 0, 0, 15120, 0, 0]
29+
30+
Time complexity: O(n)
31+
- Calculating the product of the elements from the left: O(n)
32+
- Calculating the product of the elements from the right: O(n)
33+
- Calculating the output: O(n)
34+
- Total: O(n)
35+
36+
Space complexity: O(n)
37+
- Storing the product of the elements from the left: O(n)
38+
- Storing the product of the elements from the right: O(n)
39+
- Storing the output: O(n)
40+
"""
41+
from typing import List
42+
43+
44+
class Solution:
45+
def productExceptSelf(self, nums: List[int]) -> List[int]:
46+
output = [0] * len(nums)
47+
48+
from_left = [0] * (len(nums))
49+
from_right = [0] * (len(nums))
50+
51+
from_left[0] = nums[0]
52+
from_right[-1] = nums[-1]
53+
54+
for i in range(1, len(nums) - 1):
55+
from_left[i] = from_left[i - 1] * nums[i]
56+
57+
for i in reversed(range(1, len(nums) - 1)):
58+
from_right[i] = from_right[i + 1] * nums[i]
59+
60+
output[0] = from_right[1]
61+
output[-1] = from_left[-2]
62+
for i in range(1, len(nums) - 1):
63+
output[i] = from_left[i - 1] * from_right[i + 1]
64+
65+
return output

top-k-frequent-elements/bhyun-kim.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
347. Top K Frequent Elements
3+
https://leetcode.com/problems/top-k-frequent-elements/description/
4+
5+
Solution:
6+
- Count the frequency of each element in the list
7+
- Sort the dictionary by the frequency in descending order
8+
- Return the first k elements in the sorted dictionary
9+
10+
Time complexity: O(nlogn)
11+
- Counting the frequency of each element: O(n)
12+
- Sorting the dictionary: O(nlogn)
13+
- Returning the first k elements: O(k)
14+
k <= n
15+
- Total: O(nlogn)
16+
17+
Space complexity: O(n)
18+
- Storing the frequency of each element: O(n)
19+
- Storing the sorted dictionary: O(n)
20+
- Storing the output: O(k)
21+
k <= n
22+
"""
23+
from collections import OrderedDict
24+
from typing import List
25+
26+
27+
class Solution:
28+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
29+
n_dict = OrderedDict()
30+
31+
for n in nums:
32+
if n in n_dict:
33+
n_dict[n] += 1
34+
else:
35+
n_dict[n] = 1
36+
37+
n_dict = sorted(n_dict.items(), key=lambda x: x[1], reverse=True)
38+
39+
output = [0] * k
40+
for i in range(k):
41+
output[i] = n_dict[i][0]
42+
43+
return output

0 commit comments

Comments
 (0)