-
-
Notifications
You must be signed in to change notification settings - Fork 244
[SeongA] Week 12 solution #1938
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,22 @@ | ||
class Solution { | ||
// Time O(n log n) | ||
// Space O(n) | ||
func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int { | ||
if intervals.count < 2 { return 0 } | ||
|
||
var answer = 0 | ||
let intervals = intervals.sorted { $0[1] < $1[1] } | ||
var currentInterval = intervals[0] | ||
|
||
for i in 1..<intervals.count { | ||
if currentInterval[1] <= intervals[i][0] { | ||
currentInterval = intervals[i] | ||
} else { | ||
answer += 1 | ||
} | ||
} | ||
|
||
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,36 @@ | ||
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 removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { | ||
var slow: ListNode? = head | ||
var fast: ListNode? = head | ||
var prev: ListNode? | ||
|
||
for _ in 1...n { | ||
fast = fast?.next | ||
} | ||
|
||
while fast != nil { | ||
prev = slow | ||
slow = slow?.next | ||
fast = fast?.next | ||
} | ||
|
||
if prev != nil { | ||
prev?.next = slow?.next | ||
} else { | ||
return head?.next | ||
} | ||
|
||
return head | ||
} | ||
} | ||
|
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,37 @@ | ||
public class TreeNode { | ||
public var val: Int | ||
public var left: TreeNode? | ||
public var right: TreeNode? | ||
public init() { self.val = 0; self.left = nil; self.right = nil; } | ||
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } | ||
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
} | ||
} | ||
|
||
class Solution { | ||
// Time O(n) | ||
// Space best O(log n) | ||
// Space worst O(n) | ||
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { | ||
return dfs(p, q) | ||
} | ||
|
||
private func dfs(_ p: TreeNode?, _ q: TreeNode?) -> Bool { | ||
if p == nil && q == nil { | ||
return true | ||
} | ||
|
||
guard let p = p else { return false } | ||
guard let q = q else { return false } | ||
|
||
if p.val != q.val { | ||
return false | ||
} | ||
|
||
return dfs(p.left, q.left) && dfs(p.right, q.right) | ||
} | ||
} | ||
|
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.
1..<intervals.count 무슨 문법인가요? 잘몰라서..
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.
안녕하세요 주말에 감기몸살로 앓아서 늦게 답변드리는 점 양해부탁드립니다.
1부터 Intervals.count - 1까지 n번 반복한다는 의미입니다.
intervals.count가 5라고 한다면, 1~4까지 총 4번 반복합니다.