Skip to content

Commit 6bc19c0

Browse files
authored
Merge pull request #99 from SamTheKorean/solution5
[SAM] 5th week solutions
2 parents 5105c56 + 58e53aa commit 6bc19c0

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

3sum/samthekorean.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# O(n^2) where there is nested loop.
2+
# O(1) where high and low are reused as constant values.
3+
4+
5+
class Solution:
6+
def threeSum(self, nums: List[int]) -> List[List[int]]:
7+
# Initialize a set to store unique triplets
8+
triplets = set()
9+
10+
# Sort the list to make two-pointer approach possible
11+
nums.sort()
12+
13+
# Iterate through the list, considering each element as a potential first element of a triplet
14+
for i in range(len(nums) - 2):
15+
low = i + 1
16+
high = len(nums) - 1
17+
18+
# To avoid duplicate iteration
19+
while low < high:
20+
three_sum = nums[i] + nums[low] + nums[high]
21+
22+
if three_sum < 0:
23+
low += 1
24+
elif three_sum > 0:
25+
high -= 1
26+
else:
27+
triplets.add((nums[i], nums[low], nums[high]))
28+
low += 1
29+
high -= 1
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
7+
# TC : O(n) where n is the combined length of the string in the list of strings.
8+
# SC : O(S), where S is the sum of the lengths of all strings in strs
9+
def encode(self, strs):
10+
res = ""
11+
for s in strs:
12+
res += str(len(s)) + ":" + s
13+
return res
14+
15+
"""
16+
@param: str: A string
17+
@return: decodes a single string to a list of strings
18+
"""
19+
20+
# TC : O(n) where n is the length of encoded string
21+
# SC : O(S), where S is the total length of the decoded strings.
22+
def decode(self, str):
23+
res, i = [], 0
24+
25+
while i < len(str):
26+
j = i
27+
while str[j] != ":":
28+
j += 1
29+
length = int(str[i:j])
30+
res.append(str[j + 1 : j + length + 1])
31+
i = j + 1 + length
32+
33+
return res
34+
35+
36+
def test_solution():
37+
solution = Solution()
38+
39+
# Test case 1: Basic test case
40+
input_strs = ["hello", "world"]
41+
encoded = solution.encode(input_strs)
42+
decoded = solution.decode(encoded)
43+
print(f"Test case 1\nEncoded: {encoded}\nDecoded: {decoded}\n")
44+
45+
# Test case 2: Empty list
46+
input_strs = []
47+
encoded = solution.encode(input_strs)
48+
decoded = solution.decode(encoded)
49+
print(f"Test case 2\nEncoded: {encoded}\nDecoded: {decoded}\n")
50+
51+
# Test case 3: List with empty strings
52+
input_strs = ["", "", ""]
53+
encoded = solution.encode(input_strs)
54+
decoded = solution.decode(encoded)
55+
print(f"Test case 3\nEncoded: {encoded}\nDecoded: {decoded}\n")
56+
57+
# Test case 4: List with mixed empty and non-empty strings
58+
input_strs = ["abc", "", "def"]
59+
encoded = solution.encode(input_strs)
60+
decoded = solution.decode(encoded)
61+
print(f"Test case 4\nEncoded: {encoded}\nDecoded: {decoded}\n")
62+
63+
# Test case 5: List with special characters
64+
input_strs = ["he:llo", "wo:rld"]
65+
encoded = solution.encode(input_strs)
66+
decoded = solution.decode(encoded)
67+
print(f"Test case 5\nEncoded: {encoded}\nDecoded: {decoded}\n")
68+
69+
70+
test_solution()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TC : O(n) where n is the length of nums
2+
# SC : O(n) where n is the size of nums
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
numSet = set(nums)
6+
longest = 0
7+
8+
for n in nums:
9+
# check whether its the start of the sequence
10+
if (n - 1) not in numSet:
11+
length = 0
12+
while (n + length) in numSet:
13+
length += 1
14+
longest = max(length, longest)
15+
return longest
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
4+
5+
class Solution:
6+
def productExceptSelf(self, nums: List[int]) -> List[int]:
7+
ans = [1] * (len(nums))
8+
9+
prefix = 1
10+
for i in range(len(nums)):
11+
ans[i] = prefix
12+
prefix *= nums[i]
13+
postfix = 1
14+
for i in range(len(nums) - 1, -1, -1):
15+
ans[i] *= postfix
16+
postfix *= nums[i]
17+
18+
return ans
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
from collections import defaultdict
4+
5+
6+
class Solution:
7+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
8+
# Initialize a list of empty lists to store numbers based on their frequencies
9+
freq = [[] for i in range(len(nums) + 1)]
10+
11+
# Dictionary to count the frequency of each number
12+
counter = defaultdict(int)
13+
14+
# Count the frequency of each number in nums
15+
for num in nums:
16+
counter[num] += 1
17+
18+
# Group numbers by their frequency
19+
for n, c in counter.items():
20+
freq[c].append(n)
21+
22+
# Result list to store the top k frequent elements
23+
res = []
24+
25+
# Iterate over the frequency list in reverse order to get the most frequent elements first
26+
for i in range(len(freq) - 1, 0, -1):
27+
for n in freq[i]:
28+
res.append(n)
29+
# Once we have k elements in the result, return it
30+
if len(res) == k:
31+
return res

0 commit comments

Comments
 (0)