Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SAM] 5th week solutions #99

Merged
merged 7 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 3sum/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions encode-and-decode-strings/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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*m) where n is the length of strings in strs. m is the average length of the strings
# SC : O(S), where S is the sum of the lengths of all strings in strs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠... μ—¬μ „νžˆ λ³΅μž‘λ„ 뢄석 κ²°κ³Όκ°€ μ’€ λ‚œν•΄ν•˜κ²Œ λŠκ»΄μ§€λŠ” 것 κ°™μ•„μš” πŸ˜…
μ‹œκ°„ λ³΅μž‘λ„μ™€ 곡간 λ³΅μž‘λ„λ₯Ό ꡳ이 λ‹€λ₯Έ κΈ°μ€€μœΌλ‘œ λΆ„μ„ν•˜λŠ” 게 μ–΄λ–€ μ˜λ―Έκ°€ 있고, λ©΄μ ‘κ΄€ μž…μž₯μ—μ„œ μ–΄λ–»κ²Œ λ°›μ•„λ“œμ—¬μ§ˆμ§€ λŒ€ν•΄μ„œ ν•œ 번 생각을 ν•΄λ³΄μ‹œλ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ‹¨μˆœνžˆ a list of stringsλ₯Ό μˆœνšŒν•˜λŠ”κ±΄λ° λ„ˆλ¬΄ λ³΅μž‘ν•˜κ²Œ μƒκ°ν–ˆλ˜ 것 κ°™μŠ΅λ‹ˆλ‹€. κ³ μ³μ„œ λ‹€μ‹œ μ»€λ°‹ν–ˆμŠ΅λ‹ˆλ‹€! λ‹€μ‹œ ν”Όλ“œλ°± κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€!

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš” 파일 μ™œ λΉ„μ–΄μžˆμ£ ..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였 λ¦¬νŠΈμ½”λ“œμ—μ„œ ν’€κ³  μ•ˆλ„£μ—ˆκ±°λ‚˜ μ €μž₯μ•ˆν•˜κ³  vscode μ’…λ£Œν–ˆλ‚˜λ³΄λ„€μš”. μΆ”κ°€ν•˜κ³  μ»€λ°‹ν–ˆμŠ΅λ‹ˆλ‹€!

Empty file.
18 changes: 18 additions & 0 deletions product-of-array-except-self/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions top-k-frequent-elements/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ³΅μž‘λ„μ— 영ν–₯은 주지 μ•Šμ§€λ§Œ Nice optimization μž…λ‹ˆλ‹€!