Skip to content

Commit

Permalink
Merge pull request #831 from rivkode/main
Browse files Browse the repository at this point in the history
[rivkode] Week4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents 2e88f27 + a0f9f14 commit 9bd4f49
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
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

# 더미 노드 다음부터 시작
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


0 comments on commit 9bd4f49

Please sign in to comment.