Skip to content

Commit

Permalink
Solve:
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-Mo-99 committed Jan 2, 2025
1 parent 19018be commit 79e797c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
27 changes: 27 additions & 0 deletions merge-two-sorted-lists/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
๏ปฟ #ํ•ด์„
#list1 ๊ณผ list2๋Š” ์ด๋ฏธ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ
#๋‘ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋…ธ๋“œ๊ฐ’์„ ๋น„๊ตํ•ด ๋” ์ž‘์€๊ฐ’์„ ์ฒซ๋ฒˆ์งธ ๋…ธ๋“œ๋กœ ์„ ํƒ
#์ดํ›„ ์„ ํƒ๋œ ๋…ธ๋“œ์˜ nextํฌ์ธํ„ฐ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋…ธ๋“œ ๋ณ‘ํ•ฉ


#Big O
#N: ๋งค๊ฐœ๋ณ€์ˆ˜ n์˜ ํฌ๊ธฐ(๊ณ„๋‹จ ๊ฐฏ์ˆ˜)

#Time Complexity: O(M + N)
#- M: list1์˜ ๊ธธ์ด
#- N: list2์˜ ๊ธธ์ด
#- ๊ฐ ์žฌ๊ท€ ํ˜ธ์ถœ์— ํ•˜๋‚˜์˜ ๋…ธ๋“œ๊ฐ€ ๋‹ค๋ค„์ง„๋‹ค, ๋”ฐ๋ผ์„œ ์ตœ๋Œ€ M+N ๋ฒˆ์˜ ์žฌ๊ท€ํ˜ธ์ถœ ๋ฐœ์ƒ


#Space Complexity: O(M+N)
#- ๋ณ‘ํ•ฉ ๋ฆฌ์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด M+N์ด๋ฏ€๋กœ, ์Šคํƒ ๊ณต๊ฐ„ ์‚ฌ์šฉ๋„ ์ด์— ๋น„๋ก€ํ•œ๋‹ค.

class Solution(object):
def mergeTwoLists(self, list1, list2):
if list1 and list2: ## ๋‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ชจ๋‘ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ํ•ด๋‹น
if list1.val > list2.val: # list1์˜ ํ˜„์žฌ ๊ฐ’์ด list2๋ณด๋‹ค ํฌ๋‹ค๋ฉด
list1, list2 = list2, list1 # ๊ฐ’์„ ๊ตํ™˜ํ•˜์—ฌ ํ•ญ์ƒ list1์ด ๋” ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง
list1.next = self.mergeTwoLists(list1.next, list2) # list1์˜ ๋‹ค์Œ ๋…ธ๋“œ์™€ list2๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ
return list1 or list2 # ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ๋น„์–ด ์žˆ๋‹ค๋ฉด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜


42 changes: 42 additions & 0 deletions missing-number/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
๏ปฟ #ํ•ด์„
#nums๊ฐ€ 0์—์„œ len(nums) ๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ํฌํ•จํ•˜๋‚˜ ํ™•์ธํ•˜๊ณ , ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ˆซ์ž๋ฅผ returnํ•œ๋‹ค.
#nums๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค
#nums๊ฐ€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด, nums[i]๋Š” ์ธ๋ฑ์Šค i์™€ ๋™์ผํ•ด์•ผํ•œ๋‹ค. (e.g nums[0] =0, nums[1]=1)
#๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ(nums(len(num-1))) ์ด len(nums)์™€ ๋™์ผํ•˜์ง€ ์•Š์œผ๋ฉด nums์€ 0~len(nums) ๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑ X -> ๋ˆ„๋ฝ๋œ len(nums)๋ฅผ returnํ•œ๋‹ค.
#for loop ๋กœ ๊ฐ ์ˆซ์ž๊ฐ€ ์ธ๋ฑ์Šค์™€ ์ผ์น˜ ์—ฌ๋ถ€ ํ™•์ธ
#์ธ๋ฑ์Šค์™€ ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” nums[i]์˜ ์ธ๋ฑ์Šค๋ฅผ returnํ•œ๋‹ค.


#Big O
#N: ๋งค๊ฐœ๋ณ€์ˆ˜ n์˜ ํฌ๊ธฐ(๊ณ„๋‹จ ๊ฐฏ์ˆ˜)

#Time Complexity: O(nlog(n)) = O(nlog(n))+O(1)+O(n)
#- n: nums๋ฐฐ์—ด์˜ ๊ธธ์ด
#- sort(): Timsort๋ฐฉ์‹์ด๊ธฐ์— O(nlog(n))
#-if(nums[len(nums)-1] != len(nums)): ๋‹จ์ผ ์กฐ๊ฑด ๋น„๊ต๋Š” O(1)
#for loop: nums์˜ ๊ธธ์ด์— ๋น„๋ก€ํ•˜๋ฏ€๋กœ O(n)


#Space Complexity: O(1)
#- sort(): nums.sort()๋Š” ์ œ์ž๋ฆฌ ์ •๋ ฌ์ด๊ธฐ์— ์ถ”๊ฐ€ ๊ณต๊ฐ„ ํ•„์š”์น˜ ์•Š์œผ๋ฏ€๋กœ O(1)

class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Sort the list
nums.sort()
# If the last element of nums doesn't align with the numbers of element in nums, return len(nums)
# For example, nums[0,1] so range is [0:2] but there's no last element of 2 so return 2(len(nums))
if(nums[len(nums)-1] != len(nums)):
return len(nums)
#If each element doesn't match with the number of [0:len(nums)], return the i(index)
for i in range(len(nums)):
if nums[i] != i:
print(nums[i],i)
return i



0 comments on commit 79e797c

Please sign in to comment.