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

[JonghunLee] Week 1 #698

Merged
merged 5 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions contains-duplicate/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# Time complexity: O(n)
# The process of traversing a list to generate a set is proportional to the length of the input list.
# Space complexity: O(n)
# The size of the set for storing deduplicated elements is proportional to the length of the input list.

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
list_len = len(nums)
set_len = len(set(nums))

return list_len != set_len

if __name__ == "__main__":
solution = Solution()

# test case
test_string = [
[1,2,3,1], # True
[1,2,3,4], # False
[1,1,1,3,3,4,3,2,4,2], # True
]

for index, test in enumerate(test_string):
print(f"start {index} test")
print(f"input : {test}")
print(f"Is valid palindrome ? {solution.containsDuplicate(test)}\n")
40 changes: 40 additions & 0 deletions top-k-frequent-elements/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import List

# Time Complexity O(n log n)
# - traversing for loop takes O(n) to create hash dictionary,
# - and when sorting by sorted function(TimSort) it takes O(nlogn)
# Space Complexity O(n log n)
# - creating hash dictionary takes O(n)
# - and when sorting takes O(n), hash[x] occupy O(1)

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
hash = dict()

# for loop nums to set count for each element in hash(dictionary)
for num in nums:
if num in hash:
hash[num] += 1
else:
hash[num] = 1

# sort (TimSort), using lambda function to set a sorting key which is a count
return sorted(hash, key=lambda x: hash[x], reverse=True)[:k]

if __name__ == "__main__":
solution = Solution()

# test case
nums_list = [
[1,1,1,2,2,3], # [1, 2]
[1] # [1]
]
k_list = [2, 1]

for i in range(2):
nums = nums_list[i]
k = k_list[i]
result = solution.topKFrequent(nums, k)
print(f"start{i}")
print(f"input : {nums}, {k}")
print(f"result : {result}")
48 changes: 48 additions & 0 deletions valid-palindrome/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re


# Time Complexity O(n)
# - Both normal preprocessing and two-pointer comparisons have time complexity proportional to the length of the input string.
# Space Complexity O(n)
# - The space of the new string joined_string preprocessed from the input string is proportional to the length of the input string.

class Solution:
def isPalindrome(self, s: str) -> bool:
# Pre-processing using regular expression
joined_string = re.sub(r"[^a-zA-Z]", "", s.lower())
str_length = len(joined_string)


# for loop n/2, two pointer for verify same or not
for i in range(str_length // 2):
end = str_length - i - 1
if not self.check_palindrome(i, end, joined_string):
return False

return True

def check_palindrome(self, i, end, joined_string) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

중요하지 않은 사항입니다. 무시하셔도 좋습니다!
특별한 이유가 없으시다면 i, j 혹은 start, end 등으로 스타일을 맞춰주시는 편이 좀 더 보기 좋고, 나중에 혹시라도 면접관으로부터 공격(?)의 여지를 덜 주지 않을까 싶어서 의견 드립니다!

Copy link
Member Author

Choose a reason for hiding this comment

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

리뷰해주셔서 감사합니다!

넵 알겠습니다ㅎㅎ 코드를 보고 명확 네이밍을 사용하도록 하겠습니다. 중요한 부분 같습니다.

left = joined_string[i]
right = joined_string[end]

if left == right:
return True
else:
return False

if __name__ == "__main__":
solution = Solution()

# test case
test_string = [
"A man, a plan, a canal: Panama", # True (회문)
"race a car", # False (회문 아님)
" ", # True (빈 문자열도 회문으로 간주)
"abba", # True (회문)
"abcba", # True (회문)
]

for index, test in enumerate(test_string):
print(f"start {index} test")
print(f"input : {test}")
print(f"Is valid palindrome ? {solution.isPalindrome(test)}\n")
Loading