-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 # ๋ฆฌ์คํธ๊ฐ ํ๋๋ผ๋ ๋น์ด ์๋ค๋ฉด ๋น์ด ์์ง ์์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํ | ||
|
||
|
This file contains 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,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 | ||
|
||
|
||
|