diff --git a/binary-tree-level-order-traversal/yhkee0404.rs b/binary-tree-level-order-traversal/yhkee0404.rs new file mode 100644 index 000000000..ba853c4df --- /dev/null +++ b/binary-tree-level-order-traversal/yhkee0404.rs @@ -0,0 +1,62 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; + +#[derive(Clone)] +struct QueueItem { + node: Option>>, + dist: i32, +} + +impl Solution { + pub fn level_order(root: Option>>) -> Vec> { + let mut ans: Vec> = vec![]; + let mut queue: Vec = vec![QueueItem::new(root, 0)]; + let mut head = 0; + while head != queue.len() { // T(n) = S(n) = O(n) + let u = queue[head].clone(); + head += 1; + match u.node { + Some(v) => { + let v = v.borrow(); + let v_dist = u.dist + 1; + if ans.len() <= u.dist as usize { + ans.push(vec![]); + } + let mut a = ans.last_mut().unwrap(); + a.push(v.val); + queue.push(QueueItem::new(v.left.clone(), v_dist)); + queue.push(QueueItem::new(v.right.clone(), v_dist)); + }, + None => (), + } + } + ans + } +} + +impl QueueItem { + fn new(node: Option>>, dist: i32) -> Self { + QueueItem { + node: node, + dist: dist, + } + } +} diff --git a/counting-bits/yhkee0404.swift b/counting-bits/yhkee0404.swift new file mode 100644 index 000000000..9ca30b985 --- /dev/null +++ b/counting-bits/yhkee0404.swift @@ -0,0 +1,9 @@ +class Solution { + func countBits(_ n: Int) -> [Int] { + var ans = Array(repeating: 0, count: n + 1) // S(n) = O(n) + for i in 1..> 1] + (i & 1) + } + return ans + } +} diff --git a/house-robber-ii/yhkee0404.scala b/house-robber-ii/yhkee0404.scala new file mode 100644 index 000000000..4bada0990 --- /dev/null +++ b/house-robber-ii/yhkee0404.scala @@ -0,0 +1,17 @@ +object Solution { + def rob(nums: Array[Int]): Int = { + if (nums.length == 1) { + return nums(0) + } + val dp = Array.fill(nums.length)(Array.fill(2)(0)) // T(n) = S(n) = O(n) + dp(0)(1) = nums(0) + dp(1)(0) = nums(1) + dp(1)(1) = nums(0) + for (i <- 2 until nums.length) { + for (j <- 0 to 1) { + dp(i)(j) = Math.max(dp(i - 1)(j), dp(i - 2)(j) + nums(i)) + } + } + Math.max(dp(nums.length - 2)(1), dp(nums.length - 1)(0)) + } +} diff --git a/meeting-rooms-ii/yhkee0404.kt b/meeting-rooms-ii/yhkee0404.kt new file mode 100644 index 000000000..79c9b5a98 --- /dev/null +++ b/meeting-rooms-ii/yhkee0404.kt @@ -0,0 +1,35 @@ +/** + * Definition of Interval: + * class Interval { + * var start: Int = 0 + * var end: Int = 0 + * constructor(start: Int, end: Int) { + * this.start = start + * this.end = end + * } + * } + */ + +class Solution { + /** + * @param intervals: an array of meeting time intervals + * @return: the minimum number of conference rooms required + */ + fun minMeetingRooms(intervals: List): Int { + // Write your code here + val points = mutableListOf>() + intervals.filterNotNull() // T(n) = O(nlogn) + .forEach { + points.add(listOf(it.start, 1,)) + points.add(listOf(it.end, -1,)) + } + var ans = 0 + var cnt = 0 + points.sortedBy {it[0]} // T(n) = S(n) = O(n) + .forEach { + cnt += it[1] + ans = maxOf(ans, cnt,) + } + return ans + } +} diff --git a/word-search-ii/yhkee0404.go b/word-search-ii/yhkee0404.go new file mode 100644 index 000000000..8855b8ab0 --- /dev/null +++ b/word-search-ii/yhkee0404.go @@ -0,0 +1,62 @@ +func findWords(board [][]byte, words []string) []string { + root := &TrieNode{} + for _, w := range words { + root.Insert(w) + } + + m, n := len(board), len(board[0]) + results := []string{} + + var dfs func(r, c int, node *TrieNode) + dfs = func(r, c int, node *TrieNode) { + ch := board[r][c] + if ch == '#' { + return + } + idx := ch - 'a' + next := node.children[idx] + if next == nil { + return + } + if next.word != "" { + results = append(results, next.word) + next.word = "" + } + + board[r][c] = '#' + for _, d := range directions { + nr, nc := r+d[0], c+d[1] + if nr >= 0 && nr < m && nc >= 0 && nc < n { + dfs(nr, nc, next) + } + } + board[r][c] = ch + } + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + dfs(i, j, root) + } + } + + return results +} + +type TrieNode struct { + children [26]*TrieNode + word string +} + +func (t *TrieNode) Insert(word string) { + node := t + for _, ch := range word { + idx := ch - 'a' + if node.children[idx] == nil { + node.children[idx] = &TrieNode{} + } + node = node.children[idx] + } + node.word = word +} + +var directions = [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}