-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from 5 commits
b35abbb
902b7be
1add9e6
863025e
284f324
380ebdd
58e53aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 | ||
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() |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ νμΌ μ λΉμ΄μμ£ ..? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ€ λ¦¬νΈμ½λμμ νκ³ μλ£μκ±°λ μ μ₯μνκ³ vscode μ’ λ£νλ보λ€μ. μΆκ°νκ³ μ»€λ°νμ΅λλ€! |
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 볡μ‘λμ μν₯μ μ£Όμ§ μμ§λ§ Nice optimization μ λλ€! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ν ... μ¬μ ν 볡μ‘λ λΆμ κ²°κ³Όκ° μ’ λν΄νκ² λκ»΄μ§λ κ² κ°μμ π
μκ° λ³΅μ‘λμ κ³΅κ° λ³΅μ‘λλ₯Ό κ΅³μ΄ λ€λ₯Έ κΈ°μ€μΌλ‘ λΆμνλ κ² μ΄λ€ μλ―Έκ° μκ³ , λ©΄μ κ΄ μ μ₯μμ μ΄λ»κ² λ°μλμ¬μ§μ§ λν΄μ ν λ² μκ°μ ν΄λ³΄μλ©΄ μ’μ κ² κ°μ΅λλ€.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¨μν a list of stringsλ₯Ό μννλκ±΄λ° λ무 볡μ‘νκ² μκ°νλ κ² κ°μ΅λλ€. κ³ μ³μ λ€μ 컀λ°νμ΅λλ€! λ€μ νΌλλ°± κ°μ¬λ립λλ€!