Skip to content

Commit 0a64f3d

Browse files
authored
Merge pull request #1942 from yhkee0404/main
[yhkee0404] WEEK 12 solutions
2 parents 0ffb3d5 + 9416bd5 commit 0a64f3d

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int {
3+
let intervals = intervals.sorted() { $0[1] < $1[1] } // T(n) = S(n) = O(nlogn)
4+
var ans = 0
5+
var end = -50_000
6+
for se in intervals {
7+
if se[0] < end {
8+
ans += 1
9+
continue
10+
}
11+
end = se[1]
12+
}
13+
return ans
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
/**
3+
* @param n: the number of vertices
4+
* @param edges: the edges of undirected graph
5+
* @return: the number of connected components
6+
*/
7+
fun countComponents(n: Int, edges: Array<IntArray>): Int {
8+
// write your code here
9+
val adj = List(n) {mutableListOf<Int>()}
10+
edges.forEach {
11+
adj[it[0]].add(it[1])
12+
adj[it[1]].add(it[0])
13+
}
14+
val visited = MutableList(n) {false} // T(V, E) = S(V, E) = O(V + E)
15+
val stack = mutableListOf<Int>()
16+
var ans = 0
17+
for (i in 0 until n) {
18+
if (visited[i]) {
19+
continue
20+
}
21+
ans++
22+
visited[i] = true
23+
stack.add(i)
24+
while (! stack.isEmpty()) {
25+
val u = stack.removeLast()
26+
adj[u].filter {! visited[it]}
27+
.forEach {
28+
visited[it] = true
29+
stack.add(it)
30+
}
31+
}
32+
}
33+
return ans
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
19+
let mut dummy = Some(Box::new(ListNode::new(0)));
20+
dummy.as_mut().unwrap().next = head;
21+
Self::solve(&mut dummy, n);
22+
dummy.unwrap().next
23+
}
24+
fn solve(head: &mut Option<Box<ListNode>>, n: i32) -> i32 {
25+
match head {
26+
None => 0,
27+
Some(u) => {
28+
let ans = Self::solve(&mut u.next, n) + 1;
29+
if ans == n + 1 {
30+
u.next = u.next.as_mut().unwrap().next.take();
31+
}
32+
ans
33+
}
34+
}
35+
}
36+
}

same-tree/yhkee0404.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
4+
* var value: Int = _value
5+
* var left: TreeNode = _left
6+
* var right: TreeNode = _right
7+
* }
8+
*/
9+
object Solution {
10+
def isSameTree(p: TreeNode, q: TreeNode): Boolean = {
11+
return p == null && q == null
12+
|| (
13+
p != null
14+
&& q != null
15+
&& p.value == q.value
16+
&& this.isSameTree(p.left, q.left)
17+
&& this.isSameTree(p.right, q.right)
18+
)
19+
}
20+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
10+
type Codec struct {
11+
12+
}
13+
14+
func Constructor() Codec {
15+
return Codec{}
16+
}
17+
18+
// Serializes a tree to a single string.
19+
func (this *Codec) serialize(root *TreeNode) string {
20+
if root == nil {
21+
return ""
22+
}
23+
type Pair struct {
24+
node *TreeNode
25+
path string
26+
}
27+
queue := []Pair{{node: root, path: "1"}}
28+
var tokens []string
29+
for len(queue) != 0 {
30+
u := queue[0]
31+
queue = queue[1:]
32+
tokens = append(tokens, fmt.Sprintf("%s:%d", u.path, u.node.Val))
33+
if u.node.Left != nil {
34+
queue = append(queue, Pair{node: u.node.Left, path: u.path + "0"})
35+
}
36+
if u.node.Right != nil {
37+
queue = append(queue, Pair{node: u.node.Right, path: u.path + "1"})
38+
}
39+
}
40+
return strings.Join(tokens, ",")
41+
}
42+
43+
// Deserializes your encoded data to tree.
44+
func (this *Codec) deserialize(data string) *TreeNode {
45+
if data == "" {
46+
return nil
47+
}
48+
nodes := map[string]*TreeNode{}
49+
for _, token := range strings.Split(data, ",") {
50+
values := strings.Split(token, ":")
51+
path := values[0]
52+
nodeVal, _ := strconv.Atoi(values[1])
53+
nodes[path] = &TreeNode{Val: nodeVal}
54+
if path == "1" {
55+
continue
56+
}
57+
if path[len(path) - 1] == '0' {
58+
nodes[path[: len(path) - 1]].Left = nodes[path]
59+
} else {
60+
nodes[path[: len(path) - 1]].Right = nodes[path]
61+
}
62+
}
63+
return nodes["1"]
64+
}
65+
66+
67+
/**
68+
* Your Codec object will be instantiated and called as such:
69+
* ser := Constructor();
70+
* deser := Constructor();
71+
* data := ser.serialize(root);
72+
* ans := deser.deserialize(data);
73+
*/

0 commit comments

Comments
 (0)