-
-
Notifications
You must be signed in to change notification settings - Fork 244
[SeongA] WEEK 11 Solutions #1930
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 all commits
Commits
Show all changes
3 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,31 @@ | ||
class Solution { | ||
// Time O(n log n) | ||
// Space O(n) | ||
func merge(_ intervals: [[Int]]) -> [[Int]] { | ||
if intervals.count <= 1 { | ||
return intervals | ||
} | ||
|
||
let intervals = intervals.sorted { $0[0] <= $1[0] } | ||
var answer: [[Int]] = [] | ||
var currentInterval: [Int] = [] | ||
|
||
for i in 0..<intervals.count { | ||
if currentInterval.isEmpty { | ||
currentInterval = intervals[i] | ||
} | ||
|
||
if intervals[i][0] >= currentInterval[0] && intervals[i][0] <= currentInterval[1] { | ||
currentInterval = [currentInterval[0], max(currentInterval[1], intervals[i][1])] | ||
} else { | ||
answer.append(currentInterval) | ||
currentInterval = intervals[i] | ||
} | ||
} | ||
|
||
answer.append(currentInterval) | ||
|
||
return answer | ||
} | ||
} | ||
|
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,14 @@ | ||
class Solution { | ||
// Time O(n) | ||
// Space O(1) | ||
func missingNumber(_ nums: [Int]) -> Int { | ||
var sum = 0 | ||
for i in 0..<nums.count { | ||
sum += i | ||
sum -= nums[i] | ||
} | ||
|
||
return sum + nums.count | ||
} | ||
} | ||
|
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,63 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public class ListNode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public var val: Int | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public var next: ListNode? | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public init() { self.val = 0; self.next = nil; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public init(_ val: Int) { self.val = val; self.next = nil; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
class Solution { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// Time O(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// Space O(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
func reorderList(_ head: ListNode?) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
guard let head = head, head.next != nil, head.next?.next != nil else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
var slow = head | ||||||||||||||||||||||||||||||||||||||||||||||||||||
var fast = head | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
while fast.next != nil && fast.next?.next != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
slow = slow.next! | ||||||||||||||||||||||||||||||||||||||||||||||||||||
fast = fast.next!.next! | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+13
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional Chaining 대신 이렇게 쓸 수도 있겠네요
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
let secondHalf = slow.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||
slow.next = nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
let reversedSecondHalf = reverseList(secondHalf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
mergeLists(head, reversedSecondHalf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
private func reverseList(_ head: ListNode?) -> ListNode? { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
var prev: ListNode? = nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||
var current = head | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
while current != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
let nextTemp = current?.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||
current?.next = prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||
prev = current | ||||||||||||||||||||||||||||||||||||||||||||||||||||
current = nextTemp | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
return prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
private func mergeLists(_ list1: ListNode?, _ list2: ListNode?) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
var first = list1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
var second = list2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
while first != nil && second != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
let firstNext = first?.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||
let secondNext = second?.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
first?.next = second | ||||||||||||||||||||||||||||||||||||||||||||||||||||
second?.next = firstNext | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
first = firstNext | ||||||||||||||||||||||||||||||||||||||||||||||||||||
second = secondNext | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
[0]
기준으로 정렬한 상태에서intervals[i][0] >= currentInterval[0]
은 항상 참일 것 같아요.왜냐하면
currentInterval = intervals[j]
라고 하면j <= i
이므로intervals[i][0] >= intervals[j][0]
가 참이기 때문이에요.또한
max
연산도 생략 가능해 보입니다.