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
62 changes: 62 additions & 0 deletions binary-tree-level-order-traversal/yhkee0404.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// 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<Rc<RefCell<TreeNode>>>,
dist: i32,
}

impl Solution {
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut ans: Vec<Vec<i32>> = vec![];
let mut queue: Vec<QueueItem> = 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<Rc<RefCell<TreeNode>>>, dist: i32) -> Self {
QueueItem {
node: node,
dist: dist,
}
}
}
9 changes: 9 additions & 0 deletions counting-bits/yhkee0404.swift
Original file line number Diff line number Diff line change
@@ -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..<n+1 { // T(n) = O(n)
ans[i] = ans[i >> 1] + (i & 1)
}
return ans
}
}
17 changes: 17 additions & 0 deletions house-robber-ii/yhkee0404.scala
Original file line number Diff line number Diff line change
@@ -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))
}
}
35 changes: 35 additions & 0 deletions meeting-rooms-ii/yhkee0404.kt
Original file line number Diff line number Diff line change
@@ -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<Interval?>): Int {
// Write your code here
val points = mutableListOf<List<Int>>()
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
}
}
62 changes: 62 additions & 0 deletions word-search-ii/yhkee0404.go
Original file line number Diff line number Diff line change
@@ -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}}