-
-
Notifications
You must be signed in to change notification settings - Fork 245
[sonjh1217] WEEK 04 solutions #1825
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,26 @@ | ||
class Solution { | ||
func findMin(_ nums: [Int]) -> Int { | ||
var low = 1 | ||
var high = nums.count - 1 | ||
|
||
while low <= high { | ||
let mid = (low + high) / 2 | ||
|
||
if nums[mid] < nums[mid - 1] { | ||
return nums[mid] | ||
} | ||
|
||
if nums[mid] > nums[0] { | ||
low = mid + 1 | ||
} else { | ||
high = mid - 1 | ||
} | ||
} | ||
|
||
return nums[0] | ||
|
||
//시간복잡도 O(logn) | ||
//공간복잡도 O(1) | ||
} | ||
} | ||
|
This file contains hidden or 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 @@ | ||
class Solution { | ||
func maxDepthStack(_ root: TreeNode?) -> Int { | ||
guard let root = root else { | ||
return 0 | ||
} | ||
var searchStack = [(TreeNode, Int)]() | ||
searchStack.append((root, 1)) | ||
var maxDepth = 1 | ||
|
||
while searchStack.isEmpty == false { | ||
guard let popped = searchStack.popLast() else { | ||
break | ||
} | ||
maxDepth = max(popped.1, maxDepth) | ||
|
||
if let left = popped.0.left { | ||
searchStack.append((left, popped.1 + 1)) | ||
} | ||
|
||
if let right = popped.0.right { | ||
searchStack.append((right, popped.1 + 1)) | ||
} | ||
} | ||
|
||
return maxDepth | ||
|
||
//시간복잡도 O(n) | ||
//공간복잡도 O(n) | ||
} | ||
|
||
func maxDepthRecursion(_ root: TreeNode?) -> Int { | ||
guard let root = root else { | ||
return 0 | ||
} | ||
|
||
return max(maxDepth(root.left), maxDepth(root.right)) + 1 | ||
|
||
//시간복잡도 O(n) | ||
//공간복잡도 O(n) | ||
} | ||
} | ||
|
This file contains hidden or 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,56 @@ | ||
class Solution { | ||
func mergeTwoListsIterative(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { | ||
var iterList1 = list1 | ||
var iterList2 = list2 | ||
|
||
var dummy = ListNode() | ||
var merged = dummy | ||
|
||
while iterList1 != nil && iterList2 != nil, | ||
let list1 = iterList1, | ||
let list2 = iterList2 { | ||
if list1.val < list2.val { | ||
merged.next = list1 | ||
iterList1 = list1.next | ||
} else { | ||
merged.next = list2 | ||
iterList2 = list2.next | ||
} | ||
|
||
guard let next = merged.next else { | ||
break | ||
} | ||
|
||
merged = next | ||
} | ||
|
||
merged.next = iterList1 ?? iterList2 | ||
|
||
return dummy.next | ||
|
||
//시간복잡도 O(m+n) | ||
//공간복잡도 O(1) | ||
} | ||
|
||
func mergeTwoListsRecursion(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { | ||
guard var list1 = list1 else { | ||
return list2 | ||
} | ||
|
||
guard var list2 = list2 else { | ||
return list1 | ||
} | ||
|
||
if list1.val < list2.val { | ||
list1.next = mergeTwoListsRecursion(list1.next, list2) | ||
return list1 | ||
} else { | ||
list2.next = mergeTwoListsRecursion(list1, list2.next) | ||
return list2 | ||
} | ||
|
||
//시간 복잡도 O(m+n) | ||
//공간 복잡도 O(m+n) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
디스코드에 Tony님이 올려주신 mid 오버플로우 관련 글도 한 번 읽어보시면 좋을 듯 합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
감사합니다! 덕분에 Swift overflow한도도 배우고, (64라서 2^(63) - 1 ≈ 9.22 × 10^18라고 합니다)
코드도 overflow 방지하는 스타일로 변경했습니다. 095850e