-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #698 from rivkode/main
[JonghunLee] Week 1
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
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,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") |
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,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}") |
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,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: | ||
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") |