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

[rivkode] Week4 #831

Merged
merged 3 commits into from
Jan 5, 2025
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
53 changes: 53 additions & 0 deletions merge-two-sorted-lists/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
# 더미 노드 생성
dummy = ListNode(-1)
current = dummy

# 두 리스트를 순회하며 병합
while list1 and list2:
if list1.val <= list2.val:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next
current = current.next

# 남아 있는 노드 처리
if list1:
current.next = list1
elif list2:
current.next = list2
Comment on lines +17 to +31
Copy link
Contributor

@taewanseoul taewanseoul Jan 3, 2025

Choose a reason for hiding this comment

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

# 두 리스트를 순회하며 병합 부분을 수정해서 # 남아 있는 노드 처리 부분을 제거할 수 있게끔 수정해 보는 것도 나쁘지 않을 것 같습니다~

Copy link
Member Author

Choose a reason for hiding this comment

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

@taewanseoul 안녕하세요 ! 리뷰 남겨주셔서 감사합니다.
혹시 그럼 남아있는 노드를 제거 하기 위해서 저 if else 부분을 while문에 같이 넣는 방법일까요 ?

로직 순서를 말씀하신건지 혹은 아예 저 남아있는 노드 처리 부분을 없애고 다른 방법을 찾아보는건지 궁금합니다 ㅎㅎ


# 더미 노드 다음부터 시작
return dummy.next

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

# test case
list1 = ListNode(1, ListNode(2, ListNode(4, )))
list2 = ListNode(1, ListNode(3, ListNode(4, )))

result = solution.mergeTwoLists(list1, list2)

while result is not None:
print(result.val)
result = result.next






38 changes: 38 additions & 0 deletions missing-number/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution(object):
# 시간복잡도 nlog(n) sort
# 공간복잡도 n 정렬시
def missingNumber_1(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
sort_nums = sorted(nums)

v = 0
for i in sort_nums:
if v != i:
return v
else:
v += 1

return v

# hash를 사용
# 모든 숫자를 dict에 입력
# for문을 돌면서 해당 hash가 dict에 존재하는지 체크
# 시간복잡도 n - for문
# 공간복잡도 n - dict 생성
def missingNumber(self, nums):
hash_keys = dict()
for i in range(len(nums) + 1):
hash_keys[i] = 0
for i in nums:
hash_keys[i] = 1

for i in range(len(nums) + 1):
if hash_keys[i] != 1:
return i

return 0


Loading