From bfb5219490ce202f8c6e892536b7e877d8fc12be Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 11 Sep 2025 22:14:07 +0900 Subject: [PATCH 1/7] reverse bits solution --- reverse-bits/yhkee0404.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 reverse-bits/yhkee0404.rs diff --git a/reverse-bits/yhkee0404.rs b/reverse-bits/yhkee0404.rs new file mode 100644 index 000000000..b431a067b --- /dev/null +++ b/reverse-bits/yhkee0404.rs @@ -0,0 +1,31 @@ +use std::sync::OnceLock; + +static _TABLE: OnceLock> = OnceLock::new(); + +impl Solution { + + pub fn reverse_bits(mut n: i32) -> i32 { + let mut x: u32 = 0; + for i in 0..6 { + let shift = if i == 5 {2} else {6} as u32; + x = x << shift | Self::init_table()[(n & (1 << shift) - 1) as usize] as u32 >> 6 - shift; + n >>= 6; + } + x as i32 + } + + fn init_table() -> &'static Vec { + _TABLE.get_or_init(|| { + let mut table: Vec = vec![0; 1 << 6]; + for (i, x) in table.iter_mut().enumerate() { + let mut j = i as i32; + for k in 0..6 { + *x = *x << 1 | j & 1; + j >>= 1; + } + } + table + }) + } + +} From 6ece05d414af70e878804bfa9276044f30a15b05 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 11 Sep 2025 22:19:34 +0900 Subject: [PATCH 2/7] reverse bits solution without explicit u32 cast --- reverse-bits/yhkee0404.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reverse-bits/yhkee0404.rs b/reverse-bits/yhkee0404.rs index b431a067b..df26af3cc 100644 --- a/reverse-bits/yhkee0404.rs +++ b/reverse-bits/yhkee0404.rs @@ -5,10 +5,10 @@ static _TABLE: OnceLock> = OnceLock::new(); impl Solution { pub fn reverse_bits(mut n: i32) -> i32 { - let mut x: u32 = 0; + let mut x = 0; for i in 0..6 { - let shift = if i == 5 {2} else {6} as u32; - x = x << shift | Self::init_table()[(n & (1 << shift) - 1) as usize] as u32 >> 6 - shift; + let shift = if i == 5 {2} else {6}; + x = x << shift | Self::init_table()[(n & (1 << shift) - 1) as usize] >> 6 - shift; n >>= 6; } x as i32 From 3f9d472970a78f3d803eac78b9c1201801dbf265 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 13 Sep 2025 14:59:32 +0900 Subject: [PATCH 3/7] longest repeating character replacement solution --- .../yhkee0404.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 longest-repeating-character-replacement/yhkee0404.kt diff --git a/longest-repeating-character-replacement/yhkee0404.kt b/longest-repeating-character-replacement/yhkee0404.kt new file mode 100644 index 000000000..856b974fc --- /dev/null +++ b/longest-repeating-character-replacement/yhkee0404.kt @@ -0,0 +1,35 @@ +class Solution { + fun characterReplacement(s: String, k: Int): Int { + val cnts = MutableList('Z'.code - 'A'.code + 1) {0} + val queue = PriorityQueue>() {a, b -> b.first() - a.first()} + queue.offer(listOf(0, -1)) + var ans = 0 + var i = 0 + var j = 0 + while (j != s.length) { // T(n) = S(n) = O(nlogn) + while (queue.size > 1) { + val u = queue.peek() + if (u.first() == cnts[u.last()]) { + break + } + queue.poll() + } + while (j != s.length) { + val diff = j - i - queue.peek().first() + if (diff > k) { + break + } + val d = s[j].code - 'A'.code + if (diff == k && queue.peek().first() != cnts[d]) { + break + } + j++ + queue.offer(listOf(++cnts[d], d)) // O(logn) + } + ans = maxOf(ans, j - i) + val d = s[i++].code - 'A'.code + queue.offer(listOf(--cnts[d], d)) // O(logn) + } + return ans + } +} From e1f412a9a002ca89a38e2d45dcfbf183ebf78c55 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 13 Sep 2025 19:28:57 +0900 Subject: [PATCH 4/7] clone graph solution --- clone-graph/yhkee0404.scala | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 clone-graph/yhkee0404.scala diff --git a/clone-graph/yhkee0404.scala b/clone-graph/yhkee0404.scala new file mode 100644 index 000000000..2e04d6fb6 --- /dev/null +++ b/clone-graph/yhkee0404.scala @@ -0,0 +1,33 @@ +import scala.collection.mutable.ListBuffer + +/** + * Definition for a Node. + * class Node(var _value: Int) { + * var value: Int = _value + * var neighbors: List[Node] = List() + * } + */ + +object Solution { + def cloneGraph(graph: Node): Node = { + if (graph == null) { + return null + } + val dp = Array.fill[Node](101)(null) + cloneGraph(dp, graph) + } + def cloneGraph(dp: Array[Node], graph: Node): Node = { + if (dp(graph.value) != null) { + return dp(graph.value) + } + val u = Node(graph.value) + dp(graph.value) = u + val neighbors = ListBuffer[Node]() + graph.neighbors + .foreach { + neighbors += cloneGraph(dp, _) + } + u.neighbors ++= neighbors + u + } +} From d0ffa801819896f22a7e95fa50e040ed0f8dbae5 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 13 Sep 2025 20:18:32 +0900 Subject: [PATCH 5/7] palindromic substrings solution --- palindromic-substrings/yhkee0404.go | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 palindromic-substrings/yhkee0404.go diff --git a/palindromic-substrings/yhkee0404.go b/palindromic-substrings/yhkee0404.go new file mode 100644 index 000000000..257a99047 --- /dev/null +++ b/palindromic-substrings/yhkee0404.go @@ -0,0 +1,38 @@ +func countSubstrings(s string) int { + runes := []rune{} // S(n) = O(n) + for _, c := range s { + runes = append(runes, c) + runes = append(runes, '\000') + } + ans := 0 + dp := make([]int, len(runes) - 1) + lastR := -1 + lastMid := -1 + i := 0 + for i != len(dp) { // Manacher T(n) = O(n) + diff := lastR - i + deviation := 0 + if diff > 0 { + deviation = min(diff, dp[lastMid - (i - lastMid)]) + } + l := i - deviation + r := i + deviation + for l != 0 && r + 1 != len(dp) && runes[l - 1] == runes[r + 1] { + deviation++ + l-- + r++ + } + dp[i] = deviation + if r > lastR { + lastR = r + lastMid = i + } + if runes[i] == '\000' { + ans += (deviation + 1) >> 1 + } else { + ans += 1 + (deviation >> 1) + } + i++ + } + return ans +} From 41fd9c01c35b6052226410dbba9085963dc49d9f Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 13 Sep 2025 20:35:06 +0900 Subject: [PATCH 6/7] longest common subsequence solution --- longest-common-subsequence/yhkee0404.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 longest-common-subsequence/yhkee0404.dart diff --git a/longest-common-subsequence/yhkee0404.dart b/longest-common-subsequence/yhkee0404.dart new file mode 100644 index 000000000..73ab87c16 --- /dev/null +++ b/longest-common-subsequence/yhkee0404.dart @@ -0,0 +1,14 @@ +class Solution { + int longestCommonSubsequence(String text1, String text2) { + final dp = List.generate( + text1.length + 1, + (_) => List.filled(text2.length + 1, 0), + ); + for (int i = 1; i <= text1.length; i++) { + for (int j = 1; j <= text2.length; j++) { + dp[i][j] = text1[i - 1] == text2[j - 1] ? dp[i - 1][j - 1] + 1 : max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[text1.length][text2.length]; + } +} From 2bb561c36f2f232961256807fcf6b2528f1e3349 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sat, 13 Sep 2025 21:13:36 +0900 Subject: [PATCH 7/7] longest repeating character replacement solution without priority queue --- .../yhkee0404.kt | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/longest-repeating-character-replacement/yhkee0404.kt b/longest-repeating-character-replacement/yhkee0404.kt index 856b974fc..95dc821ad 100644 --- a/longest-repeating-character-replacement/yhkee0404.kt +++ b/longest-repeating-character-replacement/yhkee0404.kt @@ -1,34 +1,26 @@ class Solution { fun characterReplacement(s: String, k: Int): Int { val cnts = MutableList('Z'.code - 'A'.code + 1) {0} - val queue = PriorityQueue>() {a, b -> b.first() - a.first()} - queue.offer(listOf(0, -1)) + var max_cnt = 0 var ans = 0 var i = 0 var j = 0 - while (j != s.length) { // T(n) = S(n) = O(nlogn) - while (queue.size > 1) { - val u = queue.peek() - if (u.first() == cnts[u.last()]) { - break - } - queue.poll() - } + while (j != s.length) { // T(n) = S(n) = O(n) while (j != s.length) { - val diff = j - i - queue.peek().first() + val diff = j - i - max_cnt if (diff > k) { break } val d = s[j].code - 'A'.code - if (diff == k && queue.peek().first() != cnts[d]) { + if (diff == k && max_cnt != cnts[d]) { break } j++ - queue.offer(listOf(++cnts[d], d)) // O(logn) + max_cnt = maxOf(max_cnt, ++cnts[d]) } ans = maxOf(ans, j - i) val d = s[i++].code - 'A'.code - queue.offer(listOf(--cnts[d], d)) // O(logn) + cnts[d]-- } return ans }