Skip to content

[yhkee0404] WEEK 04 solutions #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 16, 2025
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
14 changes: 14 additions & 0 deletions coin-change/yhkee0404.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
int coinChange(List<int> coins, int amount) {
List<int> dp = List.filled(amount + 1, amount + 1);
// S(n) = O(amount)
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
// T(n) = O(amount * coins.length)
dp[i] = 1 + coins.where((x) => x <= i)
.map((x) => dp[i - x])
.fold(amount, (a, b) => min(a, b));
}
return dp[amount] <= amount ? dp[amount] : -1;
}
}
16 changes: 16 additions & 0 deletions find-minimum-in-rotated-sorted-array/yhkee0404.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object Solution {
def findMin(nums: Array[Int]): Int = {
var l = 0
var r = nums.size
while (l != r) {
val m = l + ((r - l) >> 1)
// T(n) = lg(n)
if (nums(m) <= nums.last) {
r = m
} else {
l = m + 1
}
}
nums(l)
}
}
31 changes: 31 additions & 0 deletions maximum-depth-of-binary-tree/yhkee0404.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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;
impl Solution {
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
match root {
None => 0,
Some(u) => {
let u = u.borrow();
1 + Self::max_depth(u.left.clone()).max(Self::max_depth(u.right.clone()))
}
}
}
}
24 changes: 24 additions & 0 deletions merge-two-sorted-lists/yhkee0404.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
ans := &ListNode{}
l1 := list1
l2 := list2
l3 := ans
for l1 != nil || l2 != nil {
if l2 == nil || l1 != nil && l1.Val < l2.Val {
l3.Next = l1
l1 = l1.Next
} else {
l3.Next = l2
l2 = l2.Next
}
l3 = l3.Next
}
return ans.Next
}
58 changes: 58 additions & 0 deletions word-search/yhkee0404.kt
Copy link
Contributor

Choose a reason for hiding this comment

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

스택을 사용해서 푸는건 생각도 못했는데 대단하시네요!
게다가 주력 언어가 파이썬이라고 하셨죠?
문제들을 모두 멋지게 풀어주셔서 많이 배웁니다 :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
val _DRCS = listOf(
listOf(0, -1),
listOf(0, 1),
listOf(-1, 0),
listOf(1, 0),
)

class Solution {
fun exist(board: Array<CharArray>, word: String): Boolean {
val offset = 'A'.code
val invertedIndex = List('z'.code - offset + 1) {mutableListOf<List<Int>>()}
board.withIndex()
.forEach { row ->
row.value
.withIndex()
.forEach {
invertedIndex[it.value.toInt() - offset].add(listOf(row.index, it.index))
}
}
val freq = MutableList(invertedIndex.size) {0}
word.map {it.toInt() - offset}
.forEach {
freq[it]++
}
if ((0 until freq.size).any {freq[it] > invertedIndex[it].size}) {
return false
}
val target = if (invertedIndex[word.first().toInt() - offset].size <= invertedIndex[word.last().toInt() - offset].size) word
else word.reversed()
val stack = invertedIndex[target.first().toInt() - offset].map {
mutableListOf(it.first(), it.last(), 0, 1)
}.toMutableList()
val visited = MutableList(board.size) {MutableList(board.first().size) {false}}
while (! stack.isEmpty()) {
val u = stack.last()
if (u[2] == 4) {
visited[u[0]][u[1]] = false
stack.removeLast()
continue
}
if (u[2] == 0) {
if (u[3] == target.length) {
return true
}
visited[u[0]][u[1]] = true
}
val drc = _DRCS[u[2]]
u[2]++
val v = mutableListOf(u[0] + drc[0], u[1] + drc[1], 0, u[3] + 1)
if (v[0] == -1 || v[0] == board.size || v[1] == -1 || v[1] == board.first().size
|| visited[v[0]][v[1]] || board[v[0]][v[1]] != target[u[3]]) {
continue
}
stack.add(v)
}
return false
}
}