Skip to content

Commit d0f0b56

Browse files
authored
Merge pull request #1958 from yhkee0404/main
[yhkee0404] WEEK 14 solutions
2 parents a536abf + 2288072 commit d0f0b56

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
22+
#[derive(Clone)]
23+
struct QueueItem {
24+
node: Option<Rc<RefCell<TreeNode>>>,
25+
dist: i32,
26+
}
27+
28+
impl Solution {
29+
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
30+
let mut ans: Vec<Vec<i32>> = vec![];
31+
let mut queue: Vec<QueueItem> = vec![QueueItem::new(root, 0)];
32+
let mut head = 0;
33+
while head != queue.len() { // T(n) = S(n) = O(n)
34+
let u = queue[head].clone();
35+
head += 1;
36+
match u.node {
37+
Some(v) => {
38+
let v = v.borrow();
39+
let v_dist = u.dist + 1;
40+
if ans.len() <= u.dist as usize {
41+
ans.push(vec![]);
42+
}
43+
let mut a = ans.last_mut().unwrap();
44+
a.push(v.val);
45+
queue.push(QueueItem::new(v.left.clone(), v_dist));
46+
queue.push(QueueItem::new(v.right.clone(), v_dist));
47+
},
48+
None => (),
49+
}
50+
}
51+
ans
52+
}
53+
}
54+
55+
impl QueueItem {
56+
fn new(node: Option<Rc<RefCell<TreeNode>>>, dist: i32) -> Self {
57+
QueueItem {
58+
node: node,
59+
dist: dist,
60+
}
61+
}
62+
}

counting-bits/yhkee0404.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
func countBits(_ n: Int) -> [Int] {
3+
var ans = Array(repeating: 0, count: n + 1) // S(n) = O(n)
4+
for i in 1..<n+1 { // T(n) = O(n)
5+
ans[i] = ans[i >> 1] + (i & 1)
6+
}
7+
return ans
8+
}
9+
}

house-robber-ii/yhkee0404.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Solution {
2+
def rob(nums: Array[Int]): Int = {
3+
if (nums.length == 1) {
4+
return nums(0)
5+
}
6+
val dp = Array.fill(nums.length)(Array.fill(2)(0)) // T(n) = S(n) = O(n)
7+
dp(0)(1) = nums(0)
8+
dp(1)(0) = nums(1)
9+
dp(1)(1) = nums(0)
10+
for (i <- 2 until nums.length) {
11+
for (j <- 0 to 1) {
12+
dp(i)(j) = Math.max(dp(i - 1)(j), dp(i - 2)(j) + nums(i))
13+
}
14+
}
15+
Math.max(dp(nums.length - 2)(1), dp(nums.length - 1)(0))
16+
}
17+
}

meeting-rooms-ii/yhkee0404.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition of Interval:
3+
* class Interval {
4+
* var start: Int = 0
5+
* var end: Int = 0
6+
* constructor(start: Int, end: Int) {
7+
* this.start = start
8+
* this.end = end
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
/**
15+
* @param intervals: an array of meeting time intervals
16+
* @return: the minimum number of conference rooms required
17+
*/
18+
fun minMeetingRooms(intervals: List<Interval?>): Int {
19+
// Write your code here
20+
val points = mutableListOf<List<Int>>()
21+
intervals.filterNotNull() // T(n) = O(nlogn)
22+
.forEach {
23+
points.add(listOf(it.start, 1,))
24+
points.add(listOf(it.end, -1,))
25+
}
26+
var ans = 0
27+
var cnt = 0
28+
points.sortedBy {it[0]} // T(n) = S(n) = O(n)
29+
.forEach {
30+
cnt += it[1]
31+
ans = maxOf(ans, cnt,)
32+
}
33+
return ans
34+
}
35+
}

word-search-ii/yhkee0404.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
func findWords(board [][]byte, words []string) []string {
2+
root := &TrieNode{}
3+
for _, w := range words {
4+
root.Insert(w)
5+
}
6+
7+
m, n := len(board), len(board[0])
8+
results := []string{}
9+
10+
var dfs func(r, c int, node *TrieNode)
11+
dfs = func(r, c int, node *TrieNode) {
12+
ch := board[r][c]
13+
if ch == '#' {
14+
return
15+
}
16+
idx := ch - 'a'
17+
next := node.children[idx]
18+
if next == nil {
19+
return
20+
}
21+
if next.word != "" {
22+
results = append(results, next.word)
23+
next.word = ""
24+
}
25+
26+
board[r][c] = '#'
27+
for _, d := range directions {
28+
nr, nc := r+d[0], c+d[1]
29+
if nr >= 0 && nr < m && nc >= 0 && nc < n {
30+
dfs(nr, nc, next)
31+
}
32+
}
33+
board[r][c] = ch
34+
}
35+
36+
for i := 0; i < m; i++ {
37+
for j := 0; j < n; j++ {
38+
dfs(i, j, root)
39+
}
40+
}
41+
42+
return results
43+
}
44+
45+
type TrieNode struct {
46+
children [26]*TrieNode
47+
word string
48+
}
49+
50+
func (t *TrieNode) Insert(word string) {
51+
node := t
52+
for _, ch := range word {
53+
idx := ch - 'a'
54+
if node.children[idx] == nil {
55+
node.children[idx] = &TrieNode{}
56+
}
57+
node = node.children[idx]
58+
}
59+
node.word = word
60+
}
61+
62+
var directions = [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}

0 commit comments

Comments
 (0)