Skip to content

Commit f22d2d0

Browse files
authored
Merge pull request #1823 from yhkee0404/main
[yhkee0404] WEEK 04 solutions
2 parents cd7337b + 4f5c769 commit f22d2d0

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

coin-change/yhkee0404.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
int coinChange(List<int> coins, int amount) {
3+
List<int> dp = List.filled(amount + 1, amount + 1);
4+
// S(n) = O(amount)
5+
dp[0] = 0;
6+
for (int i = 1; i <= amount; i++) {
7+
// T(n) = O(amount * coins.length)
8+
dp[i] = 1 + coins.where((x) => x <= i)
9+
.map((x) => dp[i - x])
10+
.fold(amount, (a, b) => min(a, b));
11+
}
12+
return dp[amount] <= amount ? dp[amount] : -1;
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Solution {
2+
def findMin(nums: Array[Int]): Int = {
3+
var l = 0
4+
var r = nums.size
5+
while (l != r) {
6+
val m = l + ((r - l) >> 1)
7+
// T(n) = lg(n)
8+
if (nums(m) <= nums.last) {
9+
r = m
10+
} else {
11+
l = m + 1
12+
}
13+
}
14+
nums(l)
15+
}
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
impl Solution {
22+
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
23+
match root {
24+
None => 0,
25+
Some(u) => {
26+
let u = u.borrow();
27+
1 + Self::max_depth(u.left.clone()).max(Self::max_depth(u.right.clone()))
28+
}
29+
}
30+
}
31+
}

merge-two-sorted-lists/yhkee0404.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
9+
ans := &ListNode{}
10+
l1 := list1
11+
l2 := list2
12+
l3 := ans
13+
for l1 != nil || l2 != nil {
14+
if l2 == nil || l1 != nil && l1.Val < l2.Val {
15+
l3.Next = l1
16+
l1 = l1.Next
17+
} else {
18+
l3.Next = l2
19+
l2 = l2.Next
20+
}
21+
l3 = l3.Next
22+
}
23+
return ans.Next
24+
}

word-search/yhkee0404.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
val _DRCS = listOf(
2+
listOf(0, -1),
3+
listOf(0, 1),
4+
listOf(-1, 0),
5+
listOf(1, 0),
6+
)
7+
8+
class Solution {
9+
fun exist(board: Array<CharArray>, word: String): Boolean {
10+
val offset = 'A'.code
11+
val invertedIndex = List('z'.code - offset + 1) {mutableListOf<List<Int>>()}
12+
board.withIndex()
13+
.forEach { row ->
14+
row.value
15+
.withIndex()
16+
.forEach {
17+
invertedIndex[it.value.toInt() - offset].add(listOf(row.index, it.index))
18+
}
19+
}
20+
val freq = MutableList(invertedIndex.size) {0}
21+
word.map {it.toInt() - offset}
22+
.forEach {
23+
freq[it]++
24+
}
25+
if ((0 until freq.size).any {freq[it] > invertedIndex[it].size}) {
26+
return false
27+
}
28+
val target = if (invertedIndex[word.first().toInt() - offset].size <= invertedIndex[word.last().toInt() - offset].size) word
29+
else word.reversed()
30+
val stack = invertedIndex[target.first().toInt() - offset].map {
31+
mutableListOf(it.first(), it.last(), 0, 1)
32+
}.toMutableList()
33+
val visited = MutableList(board.size) {MutableList(board.first().size) {false}}
34+
while (! stack.isEmpty()) {
35+
val u = stack.last()
36+
if (u[2] == 4) {
37+
visited[u[0]][u[1]] = false
38+
stack.removeLast()
39+
continue
40+
}
41+
if (u[2] == 0) {
42+
if (u[3] == target.length) {
43+
return true
44+
}
45+
visited[u[0]][u[1]] = true
46+
}
47+
val drc = _DRCS[u[2]]
48+
u[2]++
49+
val v = mutableListOf(u[0] + drc[0], u[1] + drc[1], 0, u[3] + 1)
50+
if (v[0] == -1 || v[0] == board.size || v[1] == -1 || v[1] == board.first().size
51+
|| visited[v[0]][v[1]] || board[v[0]][v[1]] != target[u[3]]) {
52+
continue
53+
}
54+
stack.add(v)
55+
}
56+
return false
57+
}
58+
}

0 commit comments

Comments
 (0)