From fe9408a7d3b08ba8931f1a208fd3a0b38b38fd89 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 18 Sep 2025 08:22:06 +0900 Subject: [PATCH 1/5] linked list cycle --- linked-list-cycle/yhkee0404.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 linked-list-cycle/yhkee0404.go diff --git a/linked-list-cycle/yhkee0404.go b/linked-list-cycle/yhkee0404.go new file mode 100644 index 000000000..b7e77c0fd --- /dev/null +++ b/linked-list-cycle/yhkee0404.go @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func hasCycle(head *ListNode) bool { + u := head + v := head + for u != nil && v != nil { + u = u.Next + if (u == nil) { + break + } + u = u.Next + v = v.Next + if u == v { + return true + } + } + return false +} From 54aee8d98fb502161533dc7ad451504f2625debc Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Fri, 19 Sep 2025 23:11:15 +0900 Subject: [PATCH 2/5] pacific atlantic water flow solution --- pacific-atlantic-water-flow/yhkee0404.scala | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 pacific-atlantic-water-flow/yhkee0404.scala diff --git a/pacific-atlantic-water-flow/yhkee0404.scala b/pacific-atlantic-water-flow/yhkee0404.scala new file mode 100644 index 000000000..be2089e89 --- /dev/null +++ b/pacific-atlantic-water-flow/yhkee0404.scala @@ -0,0 +1,37 @@ +object Solution { + val _DRCS = Array( + Array(-1, 0), + Array(0, -1), + Array(0, 1), + Array(1, 0), + ) + def pacificAtlantic(heights: Array[Array[Int]]): List[List[Int]] = { + val visited = Array.fill[Array[Int]](heights.length)( + Array.fill[Int](heights.head.length)(0) + ) + for (i <- heights.indices) { + dfs(heights, visited, i, 0, 1) + dfs(heights, visited, i, heights.head.length - 1, 2) + } + for (i <- heights.head.indices) { + dfs(heights, visited, 0, i, 1) + dfs(heights, visited, heights.length - 1, i, 2) + } + ( + for { + r <- visited.indices + c <- visited.head.indices + if visited(r)(c) == 3 + } yield List(r, c) + ).toList + } + def dfs(heights: Array[Array[Int]], visited: Array[Array[Int]], r: Int, c: Int, v: Int): Unit = { + visited(r)(c) |= v + _DRCS.map { case Array(dr, dc) => (r + dr, c + dc) } + .filter { case (nr, nc) => nr != -1 && nr != heights.length && nc != -1 && nc != heights.head.length } + .filter { case (nr, nc) => (visited(nr)(nc) & v) == 0 && heights(r)(c) <= heights(nr)(nc) } + .foreach { case (nr, nc) => + dfs(heights, visited, nr, nc, v) + } + } +} From 96dedf8e2705daaefa6eca0c0d941013d11a5f46 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 20 Sep 2025 08:53:36 +0900 Subject: [PATCH 3/5] maximum product subarray solution --- maximum-product-subarray/yhkee0404.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 maximum-product-subarray/yhkee0404.rs diff --git a/maximum-product-subarray/yhkee0404.rs b/maximum-product-subarray/yhkee0404.rs new file mode 100644 index 000000000..33f608ea9 --- /dev/null +++ b/maximum-product-subarray/yhkee0404.rs @@ -0,0 +1,26 @@ +impl Solution { + pub fn max_product(nums: Vec) -> i32 { + let mut neg_abs_min = 1; + let mut total = 1; + let mut ans = *nums.first() + .unwrap(); + for num in nums { + if num == 0 { + neg_abs_min = 1; + total = 1; + ans = ans.max(0); + continue + } + total *= num; + ans = ans.max(total); + if total < 0 { + if neg_abs_min == 1 { + neg_abs_min = total; + } else { + ans = ans.max(total / neg_abs_min) + } + } + } + ans + } +} From e9e655febc01f0066d49785859c1779559b80972 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 20 Sep 2025 11:54:23 +0900 Subject: [PATCH 4/5] sum of two integers solution --- sum-of-two-integers/yhkee0404.dart | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sum-of-two-integers/yhkee0404.dart diff --git a/sum-of-two-integers/yhkee0404.dart b/sum-of-two-integers/yhkee0404.dart new file mode 100644 index 000000000..989b9e441 --- /dev/null +++ b/sum-of-two-integers/yhkee0404.dart @@ -0,0 +1,27 @@ +class Solution { + int getSum(int a, int b) { + int ans = 0; + // for (int i = 0, carry = 0, ai, bi, u = 1; i != 64 && (carry | a | b) != 0; i++, a >>>= 1, b >>>= 1, u <<= 1) { // significand 53 bits but u has only 1 bit + for (int i = 0, carry = 0, ai, bi, u = 1; i != 32 && (carry | a | b) != 0; i++, a >>= 1, b >>= 1, u <<= 1) { + ai = a & 1; + bi = b & 1; + if ((ai & bi) == 1) { + if (carry == 1) { + ans |= u; + } + carry = 1; + } else if ((ai ^ bi) == 1) { + if (carry == 0) { + ans |= u; + } + } else { + if (carry == 1) { + ans |= u; + } + carry = 0; + } + } + // return ans; + return (ans & 0x80000000) != 0 ? ans | ~ 0xFFFFFFFF : ans; + } +} From 29ec6d39ae8a6d917cf92ef2de7f761fb8d1d2fb Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 20 Sep 2025 12:45:48 +0900 Subject: [PATCH 5/5] minimum window substring solution --- minimum-window-substring/yhkee0404.kt | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 minimum-window-substring/yhkee0404.kt diff --git a/minimum-window-substring/yhkee0404.kt b/minimum-window-substring/yhkee0404.kt new file mode 100644 index 000000000..c530e1391 --- /dev/null +++ b/minimum-window-substring/yhkee0404.kt @@ -0,0 +1,33 @@ +class Solution { + fun minWindow(s: String, t: String): String { // T(m, n) = S(m, n) = O(m + n) + val counterT = ( + t + .groupingBy {it} // O(n) + .eachCount() + ) + var l = -1 + var r = -1 + val counterS = mutableMapOf() + var i = -1 + ( + s + .withIndex() + .forEach { // O(m) + counterS.compute(it.value) {k, v -> if (v == null) 1 else v + 1} + while (i != it.index && counterS[s[i + 1]]!! > counterT.getOrDefault(s[i + 1], 0)) { // O(m) + counterS.compute(s[++i]) {k, v -> v!! - 1} + } + if ( + counterT + .all {(k, v) -> counterS.getOrDefault(k, 0) >= v} // O(n) + ) { + if (r == -1 || r - l > it.index - i) { + l = i + r = it.index + } + } + } + ) + return s.substring(l + 1, r + 1) // O(m) + } +}