-
-
Notifications
You must be signed in to change notification settings - Fork 195
[thispath98] Week 4 #821
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
Merged
Merged
[thispath98] Week 4 #821
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e488dfb
feat: Add Merge Two Sorted Lists solutions
thispath98 5b77982
feat: Add Merge Two Sorted Lists another solutions
thispath98 4b57613
feat: Add Missing Number solutions
thispath98 bf45649
feat: Add Word Search solutions
thispath98 a338957
feat: Add Palindromic Substrings solutions
thispath98 3e8a753
feat: Add Coin Change solutions
thispath98 a30cffa
feat: Add additional soultions for Palindromic Substrings
thispath98 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 hidden or 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,86 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def mergeTwoListsList(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
""" | ||
Intuition: | ||
두 리스트의 원소를 각각 비교하면서 한번씩 스캔한다. | ||
결과적으로 한번씩만 스캔하면 정렬할 수 있다. | ||
|
||
Time Complexity: | ||
O(N): | ||
두개의 리스트를 1번 순회하며 답을 찾으므로, | ||
O(N)의 시간복잡도가 소요된다. | ||
|
||
Space Complexity: | ||
O(N): | ||
sorted_list에 정렬된 배열을 저장하므로, | ||
O(N)의 공간복잡도가 소요된다. | ||
""" | ||
sorted_list = [] | ||
while list1 is not None and list2 is not None: | ||
if list1.val < list2.val: | ||
sorted_list.append(list1.val) | ||
list1 = list1.next | ||
else: | ||
sorted_list.append(list2.val) | ||
list2 = list2.next | ||
|
||
while list1 is not None: | ||
sorted_list.append(list1.val) | ||
list1 = list1.next | ||
while list2 is not None: | ||
sorted_list.append(list2.val) | ||
list2 = list2.next | ||
|
||
sorted_node = None | ||
while sorted_list: | ||
val = sorted_list.pop() | ||
sorted_node = ListNode(val, sorted_node) | ||
|
||
return sorted_node | ||
|
||
def mergeTwoListsNode(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
""" | ||
Intuition: | ||
파이썬 리스트를 사용하지 않고 | ||
주어진 ListNode로부터 바로 시작한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
두개의 리스트를 1번 순회하며 답을 찾으므로, | ||
O(N)의 시간복잡도가 소요된다. | ||
|
||
Space Complexity: | ||
O(1): | ||
ListNode를 바로 사용하므로 | ||
상수 만큼의 O(1)의 공간복잡도가 소요된다. | ||
|
||
Key takeaway: | ||
링크드 리스트를 오랜만에 접하니 잘 풀지 못했던 것 같다. | ||
전통적인 자료구조를 OOP 관점으로 고민해보자. | ||
""" | ||
sorted_node = ListNode() | ||
current_node = sorted_node | ||
|
||
while True: | ||
if list1 is None: | ||
current_node.next = list2 | ||
break | ||
elif list2 is None: | ||
current_node.next = list1 | ||
break | ||
|
||
if list1.val < list2.val: | ||
current_node.next = ListNode(list1.val) | ||
current_node = current_node.next | ||
list1 = list1.next | ||
else: | ||
current_node.next = ListNode(list2.val) | ||
current_node = current_node.next | ||
list2 = list2.next | ||
|
||
return sorted_node.next |
This file contains hidden or 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,23 @@ | ||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
""" | ||
Intuition: | ||
주어진 리스트의 개수를 얻어 범위를 구한다. | ||
이후 세트를 이용해서 범위 내의 정수가 | ||
세트 안에 없으면 그 수를 리턴한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
세트(해시)는 접근하는 데에 상수의 시간이 걸리므로 | ||
최대 N + 1번의 접근을 하므로 | ||
O(N)의 시간복잡도가 소요된다. | ||
|
||
Space Complexity: | ||
O(N): | ||
리스트를 해시로 변환하여 저장하고 있으므로 | ||
O(N)의 공간복잡도가 소요된다. | ||
""" | ||
num_set = set(nums) | ||
for i in range(len(nums) + 1): | ||
if i not in num_set: | ||
return i | ||
Comment on lines
+20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드 직관적이라서 정말 좋아요 👍 |
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.
while True
로 주셨는데, 위의 코드에 남겨주신 것처럼 조건 명시하면 더 좋을 것 같아요 ~while list1 is not None and list2 is not None:
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.
감사합니다. 무한 루프는 최소화해보겠습니다!