diff --git a/coin-change/yhkee0404.dart b/coin-change/yhkee0404.dart new file mode 100644 index 0000000000..e0f7458d25 --- /dev/null +++ b/coin-change/yhkee0404.dart @@ -0,0 +1,14 @@ +class Solution { + int coinChange(List coins, int amount) { + List 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; + } +} diff --git a/find-minimum-in-rotated-sorted-array/yhkee0404.scala b/find-minimum-in-rotated-sorted-array/yhkee0404.scala new file mode 100644 index 0000000000..3946d5fe0c --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/yhkee0404.scala @@ -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) + } +} diff --git a/maximum-depth-of-binary-tree/yhkee0404.rs b/maximum-depth-of-binary-tree/yhkee0404.rs new file mode 100644 index 0000000000..9ce7351779 --- /dev/null +++ b/maximum-depth-of-binary-tree/yhkee0404.rs @@ -0,0 +1,31 @@ +// 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; +impl Solution { + pub fn max_depth(root: Option>>) -> 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())) + } + } + } +} diff --git a/merge-two-sorted-lists/yhkee0404.go b/merge-two-sorted-lists/yhkee0404.go new file mode 100644 index 0000000000..215e69dd6b --- /dev/null +++ b/merge-two-sorted-lists/yhkee0404.go @@ -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 +} diff --git a/word-search/yhkee0404.kt b/word-search/yhkee0404.kt new file mode 100644 index 0000000000..493f5c4db4 --- /dev/null +++ b/word-search/yhkee0404.kt @@ -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, word: String): Boolean { + val offset = 'A'.code + val invertedIndex = List('z'.code - offset + 1) {mutableListOf>()} + 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 + } +}