Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions non-overlapping-intervals/yhkee0404.swift
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 number-of-connected-components-in-an-undirected-graph/yhkee0404.kt
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
}
}
36 changes: 36 additions & 0 deletions remove-nth-node-from-end-of-list/yhkee0404.rs
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
}
}
}
}
20 changes: 20 additions & 0 deletions same-tree/yhkee0404.scala
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)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한줄로 쫙 풀어내셨군요!

}
}
73 changes: 73 additions & 0 deletions serialize-and-deserialize-binary-tree/yhkee0404.go
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);
*/