Skip to content

Commit 1e9cacd

Browse files
authored
Merge pull request #698 from rivkode/main
[JonghunLee] Week 1
2 parents b3917f5 + a085b49 commit 1e9cacd

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

contains-duplicate/rivkode.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# Time complexity: O(n)
3+
# The process of traversing a list to generate a set is proportional to the length of the input list.
4+
# Space complexity: O(n)
5+
# The size of the set for storing deduplicated elements is proportional to the length of the input list.
6+
7+
class Solution:
8+
def containsDuplicate(self, nums: list[int]) -> bool:
9+
list_len = len(nums)
10+
set_len = len(set(nums))
11+
12+
return list_len != set_len
13+
14+
if __name__ == "__main__":
15+
solution = Solution()
16+
17+
# test case
18+
test_string = [
19+
[1,2,3,1], # True
20+
[1,2,3,4], # False
21+
[1,1,1,3,3,4,3,2,4,2], # True
22+
]
23+
24+
for index, test in enumerate(test_string):
25+
print(f"start {index} test")
26+
print(f"input : {test}")
27+
print(f"Is valid palindrome ? {solution.containsDuplicate(test)}\n")

top-k-frequent-elements/rivkode.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List
2+
3+
# Time Complexity O(n log n)
4+
# - traversing for loop takes O(n) to create hash dictionary,
5+
# - and when sorting by sorted function(TimSort) it takes O(nlogn)
6+
# Space Complexity O(n log n)
7+
# - creating hash dictionary takes O(n)
8+
# - and when sorting takes O(n), hash[x] occupy O(1)
9+
10+
class Solution:
11+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
12+
hash = dict()
13+
14+
# for loop nums to set count for each element in hash(dictionary)
15+
for num in nums:
16+
if num in hash:
17+
hash[num] += 1
18+
else:
19+
hash[num] = 1
20+
21+
# sort (TimSort), using lambda function to set a sorting key which is a count
22+
return sorted(hash, key=lambda x: hash[x], reverse=True)[:k]
23+
24+
if __name__ == "__main__":
25+
solution = Solution()
26+
27+
# test case
28+
nums_list = [
29+
[1,1,1,2,2,3], # [1, 2]
30+
[1] # [1]
31+
]
32+
k_list = [2, 1]
33+
34+
for i in range(2):
35+
nums = nums_list[i]
36+
k = k_list[i]
37+
result = solution.topKFrequent(nums, k)
38+
print(f"start{i}")
39+
print(f"input : {nums}, {k}")
40+
print(f"result : {result}")

valid-palindrome/rivkode.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import re
2+
3+
4+
# Time Complexity O(n)
5+
# - Both normal preprocessing and two-pointer comparisons have time complexity proportional to the length of the input string.
6+
# Space Complexity O(n)
7+
# - The space of the new string joined_string preprocessed from the input string is proportional to the length of the input string.
8+
9+
class Solution:
10+
def isPalindrome(self, s: str) -> bool:
11+
# Pre-processing using regular expression
12+
joined_string = re.sub(r"[^a-zA-Z]", "", s.lower())
13+
str_length = len(joined_string)
14+
15+
16+
# for loop n/2, two pointer for verify same or not
17+
for i in range(str_length // 2):
18+
end = str_length - i - 1
19+
if not self.check_palindrome(i, end, joined_string):
20+
return False
21+
22+
return True
23+
24+
def check_palindrome(self, i, end, joined_string) -> bool:
25+
left = joined_string[i]
26+
right = joined_string[end]
27+
28+
if left == right:
29+
return True
30+
else:
31+
return False
32+
33+
if __name__ == "__main__":
34+
solution = Solution()
35+
36+
# test case
37+
test_string = [
38+
"A man, a plan, a canal: Panama", # True (회문)
39+
"race a car", # False (회문 아님)
40+
" ", # True (빈 문자열도 회문으로 간주)
41+
"abba", # True (회문)
42+
"abcba", # True (회문)
43+
]
44+
45+
for index, test in enumerate(test_string):
46+
print(f"start {index} test")
47+
print(f"input : {test}")
48+
print(f"Is valid palindrome ? {solution.isPalindrome(test)}\n")

0 commit comments

Comments
 (0)