-
-
Notifications
You must be signed in to change notification settings - Fork 244
[yhkee0404] WEEK 12 solutions #1942
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
5 commits
Select commit
Hold shift + click to select a range
74835d5
same tree solution
yhkee0404 4a7fd4a
remove nth node from end of list solution
yhkee0404 df607f9
number of connected components in an undirected graph solution
yhkee0404 cac2aa6
non-overlapping intervals solution
yhkee0404 9416bd5
serialize and deserialize binary tree solution
yhkee0404 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,15 @@ | ||
class Solution { | ||
func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int { | ||
let intervals = intervals.sorted() { $0[1] < $1[1] } // T(n) = S(n) = O(nlogn) | ||
var ans = 0 | ||
var end = -50_000 | ||
for se in intervals { | ||
if se[0] < end { | ||
ans += 1 | ||
continue | ||
} | ||
end = se[1] | ||
} | ||
return ans | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
number-of-connected-components-in-an-undirected-graph/yhkee0404.kt
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,35 @@ | ||
class Solution { | ||
/** | ||
* @param n: the number of vertices | ||
* @param edges: the edges of undirected graph | ||
* @return: the number of connected components | ||
*/ | ||
fun countComponents(n: Int, edges: Array<IntArray>): Int { | ||
// write your code here | ||
val adj = List(n) {mutableListOf<Int>()} | ||
edges.forEach { | ||
adj[it[0]].add(it[1]) | ||
adj[it[1]].add(it[0]) | ||
} | ||
val visited = MutableList(n) {false} // T(V, E) = S(V, E) = O(V + E) | ||
val stack = mutableListOf<Int>() | ||
var ans = 0 | ||
for (i in 0 until n) { | ||
if (visited[i]) { | ||
continue | ||
} | ||
ans++ | ||
visited[i] = true | ||
stack.add(i) | ||
while (! stack.isEmpty()) { | ||
val u = stack.removeLast() | ||
adj[u].filter {! visited[it]} | ||
.forEach { | ||
visited[it] = true | ||
stack.add(it) | ||
} | ||
} | ||
} | ||
return ans | ||
} | ||
} |
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 @@ | ||
// Definition for singly-linked list. | ||
// #[derive(PartialEq, Eq, Clone, Debug)] | ||
// pub struct ListNode { | ||
// pub val: i32, | ||
// pub next: Option<Box<ListNode>> | ||
// } | ||
// | ||
// impl ListNode { | ||
// #[inline] | ||
// fn new(val: i32) -> Self { | ||
// ListNode { | ||
// next: None, | ||
// val | ||
// } | ||
// } | ||
// } | ||
impl Solution { | ||
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> { | ||
let mut dummy = Some(Box::new(ListNode::new(0))); | ||
dummy.as_mut().unwrap().next = head; | ||
Self::solve(&mut dummy, n); | ||
dummy.unwrap().next | ||
} | ||
fn solve(head: &mut Option<Box<ListNode>>, n: i32) -> i32 { | ||
match head { | ||
None => 0, | ||
Some(u) => { | ||
let ans = Self::solve(&mut u.next, n) + 1; | ||
if ans == n + 1 { | ||
u.next = u.next.as_mut().unwrap().next.take(); | ||
} | ||
ans | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { | ||
* var value: Int = _value | ||
* var left: TreeNode = _left | ||
* var right: TreeNode = _right | ||
* } | ||
*/ | ||
object Solution { | ||
def isSameTree(p: TreeNode, q: TreeNode): Boolean = { | ||
return p == null && q == null | ||
|| ( | ||
p != null | ||
&& q != null | ||
&& p.value == q.value | ||
&& this.isSameTree(p.left, q.left) | ||
&& this.isSameTree(p.right, q.right) | ||
) | ||
} | ||
} |
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,73 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
|
||
type Codec struct { | ||
|
||
} | ||
|
||
func Constructor() Codec { | ||
return Codec{} | ||
} | ||
|
||
// Serializes a tree to a single string. | ||
func (this *Codec) serialize(root *TreeNode) string { | ||
if root == nil { | ||
return "" | ||
} | ||
type Pair struct { | ||
node *TreeNode | ||
path string | ||
} | ||
queue := []Pair{{node: root, path: "1"}} | ||
var tokens []string | ||
for len(queue) != 0 { | ||
u := queue[0] | ||
queue = queue[1:] | ||
tokens = append(tokens, fmt.Sprintf("%s:%d", u.path, u.node.Val)) | ||
if u.node.Left != nil { | ||
queue = append(queue, Pair{node: u.node.Left, path: u.path + "0"}) | ||
} | ||
if u.node.Right != nil { | ||
queue = append(queue, Pair{node: u.node.Right, path: u.path + "1"}) | ||
} | ||
} | ||
return strings.Join(tokens, ",") | ||
} | ||
|
||
// Deserializes your encoded data to tree. | ||
func (this *Codec) deserialize(data string) *TreeNode { | ||
if data == "" { | ||
return nil | ||
} | ||
nodes := map[string]*TreeNode{} | ||
for _, token := range strings.Split(data, ",") { | ||
values := strings.Split(token, ":") | ||
path := values[0] | ||
nodeVal, _ := strconv.Atoi(values[1]) | ||
nodes[path] = &TreeNode{Val: nodeVal} | ||
if path == "1" { | ||
continue | ||
} | ||
if path[len(path) - 1] == '0' { | ||
nodes[path[: len(path) - 1]].Left = nodes[path] | ||
} else { | ||
nodes[path[: len(path) - 1]].Right = nodes[path] | ||
} | ||
} | ||
return nodes["1"] | ||
} | ||
|
||
|
||
/** | ||
* Your Codec object will be instantiated and called as such: | ||
* ser := Constructor(); | ||
* deser := Constructor(); | ||
* data := ser.serialize(root); | ||
* ans := deser.deserialize(data); | ||
*/ |
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.
한줄로 쫙 풀어내셨군요!