-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b35abbb
solved top k frequent elements
SamTheKorean 902b7be
solved encode and decode strings
SamTheKorean 1add9e6
solve product of array except self
SamTheKorean 863025e
solve 3sum
SamTheKorean 284f324
solve longest consecutive sequence
SamTheKorean 380ebdd
changed time compliexity analysis for encode and decode strings
SamTheKorean 58e53aa
solved longest consecutive sequence
SamTheKorean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) 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() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 μ λλ€! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
μ€ λ¦¬νΈμ½λμμ νκ³ μλ£μκ±°λ μ μ₯μνκ³ vscode μ’ λ£νλ보λ€μ. μΆκ°νκ³ μ»€λ°νμ΅λλ€!