-
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
[JonghunLee] Week 1 #698
Merged
Merged
[JonghunLee] Week 1 #698
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,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") |
Oops, something went wrong.
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.
중요하지 않은 사항입니다. 무시하셔도 좋습니다!
특별한 이유가 없으시다면
i
,j
혹은start
,end
등으로 스타일을 맞춰주시는 편이 좀 더 보기 좋고, 나중에 혹시라도 면접관으로부터 공격(?)의 여지를 덜 주지 않을까 싶어서 의견 드립니다!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.
리뷰해주셔서 감사합니다!
넵 알겠습니다ㅎㅎ 코드를 보고 명확 네이밍을 사용하도록 하겠습니다. 중요한 부분 같습니다.