From 65f77b6b5985e029950b09896d80d52a259eee75 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Wed, 13 Aug 2025 17:23:01 +0900 Subject: [PATCH 1/5] merge two sorted lists solution --- merge-two-sorted-lists/yhkee0404.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 merge-two-sorted-lists/yhkee0404.go diff --git a/merge-two-sorted-lists/yhkee0404.go b/merge-two-sorted-lists/yhkee0404.go new file mode 100644 index 000000000..215e69dd6 --- /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 +} From 8c9f7dfc8f91ded9d8699ba34842c32a46733a85 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Wed, 13 Aug 2025 23:44:16 +0900 Subject: [PATCH 2/5] maximum depth of binary tree solution --- maximum-depth-of-binary-tree/yhkee0404.rs | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maximum-depth-of-binary-tree/yhkee0404.rs diff --git a/maximum-depth-of-binary-tree/yhkee0404.rs b/maximum-depth-of-binary-tree/yhkee0404.rs new file mode 100644 index 000000000..9ce735177 --- /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())) + } + } + } +} From d33110dbf6c18b10d7aad68d9ec5fab37541db61 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 14 Aug 2025 23:23:12 +0900 Subject: [PATCH 3/5] find minimum in rotated sorted array solution --- .../yhkee0404.scala | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/yhkee0404.scala 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 000000000..3946d5fe0 --- /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) + } +} From c1769ebf24a30400bd397db14e0ee126c269bf74 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Fri, 15 Aug 2025 00:52:44 +0900 Subject: [PATCH 4/5] word search solution --- word-search/yhkee0404.kt | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 word-search/yhkee0404.kt diff --git a/word-search/yhkee0404.kt b/word-search/yhkee0404.kt new file mode 100644 index 000000000..493f5c4db --- /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 + } +} From 4f5c769ba6a3a6f41fe263843952a125ac4f1317 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Fri, 15 Aug 2025 01:18:59 +0900 Subject: [PATCH 5/5] coin change solution --- coin-change/yhkee0404.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 coin-change/yhkee0404.dart diff --git a/coin-change/yhkee0404.dart b/coin-change/yhkee0404.dart new file mode 100644 index 000000000..e0f7458d2 --- /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; + } +}