diff --git a/non-overlapping-intervals/yhkee0404.swift b/non-overlapping-intervals/yhkee0404.swift new file mode 100644 index 000000000..e87ed8f06 --- /dev/null +++ b/non-overlapping-intervals/yhkee0404.swift @@ -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 + } +} diff --git a/number-of-connected-components-in-an-undirected-graph/yhkee0404.kt b/number-of-connected-components-in-an-undirected-graph/yhkee0404.kt new file mode 100644 index 000000000..e7a71a479 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/yhkee0404.kt @@ -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): Int { + // write your code here + val adj = List(n) {mutableListOf()} + 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() + 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 + } +} diff --git a/remove-nth-node-from-end-of-list/yhkee0404.rs b/remove-nth-node-from-end-of-list/yhkee0404.rs new file mode 100644 index 000000000..ff7de692d --- /dev/null +++ b/remove-nth-node-from-end-of-list/yhkee0404.rs @@ -0,0 +1,36 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn remove_nth_from_end(head: Option>, n: i32) -> Option> { + 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>, 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 + } + } + } +} diff --git a/same-tree/yhkee0404.scala b/same-tree/yhkee0404.scala new file mode 100644 index 000000000..c1f4376fe --- /dev/null +++ b/same-tree/yhkee0404.scala @@ -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) + ) + } +} diff --git a/serialize-and-deserialize-binary-tree/yhkee0404.go b/serialize-and-deserialize-binary-tree/yhkee0404.go new file mode 100644 index 000000000..f2da5a9ba --- /dev/null +++ b/serialize-and-deserialize-binary-tree/yhkee0404.go @@ -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); + */