Skip to content

[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 7 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
86 changes: 86 additions & 0 deletions merge-two-sorted-lists/thispath98.py
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
Comment on lines +69 to +75
Copy link
Contributor

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:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

감사합니다. 무한 루프는 최소화해보겠습니다!


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
23 changes: 23 additions & 0 deletions missing-number/thispath98.py
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
Copy link
Contributor

Choose a reason for hiding this comment

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

코드 직관적이라서 정말 좋아요 👍

Loading