From f938d33bf58a76015c9ad7b14e3a7a0ae06ddc5e Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Mon, 25 Aug 2025 07:30:09 +0900 Subject: [PATCH 1/9] valid parentheses solution --- valid-parentheses/yhkee0404.scala | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 valid-parentheses/yhkee0404.scala diff --git a/valid-parentheses/yhkee0404.scala b/valid-parentheses/yhkee0404.scala new file mode 100644 index 000000000..d7fefef18 --- /dev/null +++ b/valid-parentheses/yhkee0404.scala @@ -0,0 +1,26 @@ +import scala.collection.mutable.ArrayBuffer + +object Solution { + var encoder = "(){}[]".zipWithIndex + .toMap + def isValid(s: String): Boolean = { + val arr = ArrayBuffer[Int]() + s.forall { c => + val x = encoder(c) + if ((x & 1) == 0) { + arr += x + true + } else { + arr.lastOption match { + case Some(y) => { + if (y == x - 1) { + arr.dropRightInPlace (1) + true + } else false + } + case None => false + } + } + } && arr.isEmpty + } +} From c2fbe6f557b7a1f6c7ca384152fa5e74b315d216 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Mon, 25 Aug 2025 21:17:39 +0900 Subject: [PATCH 2/9] container with most water solution --- container-with-most-water/yhkee0404.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 container-with-most-water/yhkee0404.go diff --git a/container-with-most-water/yhkee0404.go b/container-with-most-water/yhkee0404.go new file mode 100644 index 000000000..90e6cefb4 --- /dev/null +++ b/container-with-most-water/yhkee0404.go @@ -0,0 +1,20 @@ +func maxArea(height []int) int { + ans := 0 + i, j := 0, len(height) - 1 + for i < j { + dh := height[i] - height[j] + dx := j - i + k := j + if dh == 0 { + i++ + j-- + } else if dh > 0 { + j-- + } else { + k = i + i++ + } + ans = max(ans, dx * height[k]) + } + return ans +} \ No newline at end of file From 8a4067d05873a8f58fea7aeeb5e886957e92ece7 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Mon, 25 Aug 2025 21:24:59 +0900 Subject: [PATCH 3/9] container with most water followed by a line at EOF --- container-with-most-water/yhkee0404.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container-with-most-water/yhkee0404.go b/container-with-most-water/yhkee0404.go index 90e6cefb4..0dcf3471e 100644 --- a/container-with-most-water/yhkee0404.go +++ b/container-with-most-water/yhkee0404.go @@ -17,4 +17,4 @@ func maxArea(height []int) int { ans = max(ans, dx * height[k]) } return ans -} \ No newline at end of file +} From b164832ee10e620a247464dad0a7ffc82cfaead4 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Wed, 27 Aug 2025 07:27:29 +0900 Subject: [PATCH 4/9] design add and search words data structure solution --- .../yhkee0404.rs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 design-add-and-search-words-data-structure/yhkee0404.rs diff --git a/design-add-and-search-words-data-structure/yhkee0404.rs b/design-add-and-search-words-data-structure/yhkee0404.rs new file mode 100644 index 000000000..0e0b98309 --- /dev/null +++ b/design-add-and-search-words-data-structure/yhkee0404.rs @@ -0,0 +1,74 @@ +use std::collections::HashMap; + +struct WordDictionary { + trie: Box, +} + +struct Trie { + ended: bool, + children: HashMap>, +} + + +/** + * `&self` means the method takes an immutable reference. + * If you need a mutable reference, change it to `&mut self` instead. + */ +impl WordDictionary { + + fn new() -> Self { + WordDictionary { + trie: Box::new(Trie::new()), + } + } + + // T(n) = S(n) = O(26n) = O(n) + fn add_word(&mut self, word: String) { + let mut t = &mut self.trie; + for c in word.chars() { + t = t.children + .entry(c) + .or_insert(Box::new(Trie::new())) + } + t.ended = true + } + + fn search(&self, word: String) -> bool { + return Self::search_from_index(&self.trie, &word) + } + + // T(n) = S(n) = ((2^2)n) = O(n) + fn search_from_index(trie: &Trie, word: &str) -> bool { + return match word.chars() + .next() { + None => trie.ended, + Some(c) => if c == '.' { + trie.children + .values() + .any(|x| Self::search_from_index(&x, &word[1..])) // Slice O(1) + } else { + match trie.children + .get(&c) { + None => false, + Some(x) => Self::search_from_index(&x, &word[1..]), // Slice O(1) + } + }, + } + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * let obj = WordDictionary::new(); + * obj.add_word(word); + * let ret_2: bool = obj.search(word); + */ + +impl Trie { + fn new() -> Self { + Trie { + ended: false, + children: HashMap::new(), + } + } +} From 9052e50a058e62be25617123e9246cc0c008b5d1 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Wed, 27 Aug 2025 07:29:50 +0900 Subject: [PATCH 5/9] design add and search words data structure solution time analysis fixed --- design-add-and-search-words-data-structure/yhkee0404.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/yhkee0404.rs b/design-add-and-search-words-data-structure/yhkee0404.rs index 0e0b98309..beb963830 100644 --- a/design-add-and-search-words-data-structure/yhkee0404.rs +++ b/design-add-and-search-words-data-structure/yhkee0404.rs @@ -37,7 +37,7 @@ impl WordDictionary { return Self::search_from_index(&self.trie, &word) } - // T(n) = S(n) = ((2^2)n) = O(n) + // T(n) = S(n) = ((26^2)n) = O(n) fn search_from_index(trie: &Trie, word: &str) -> bool { return match word.chars() .next() { From 7997785221dfddab63f64a9cd1bafc2ceaab5b4a Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 28 Aug 2025 20:28:55 +0900 Subject: [PATCH 6/9] longest increasing subsequence solution --- longest-increasing-subsequence/yhkee0404.dart | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-increasing-subsequence/yhkee0404.dart diff --git a/longest-increasing-subsequence/yhkee0404.dart b/longest-increasing-subsequence/yhkee0404.dart new file mode 100644 index 000000000..a6f2a346b --- /dev/null +++ b/longest-increasing-subsequence/yhkee0404.dart @@ -0,0 +1,22 @@ +class Solution { + int lengthOfLIS(List nums) { + final stack = []; // S(n) = O(n) + nums.forEach((x) { // T(n) = O(nlgn) + int i = -1, j = stack.length - 1; + while (i != j) { // O(lgn) + final int mid = j - ((j - i) >> 1); + if (stack[mid] < x) { + i = mid; + } else { + j = mid - 1; + } + } + if (++i == stack.length) { + stack.add(x); + } else { + stack[i] = x; + } + }); + return stack.length; + } +} \ No newline at end of file From 0238f39deae079705feb14b1dafc2f53fca6c031 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 28 Aug 2025 21:54:12 +0900 Subject: [PATCH 7/9] longest increasing subsequence followed by a line at EOF --- longest-increasing-subsequence/yhkee0404.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-increasing-subsequence/yhkee0404.dart b/longest-increasing-subsequence/yhkee0404.dart index a6f2a346b..b88c382ea 100644 --- a/longest-increasing-subsequence/yhkee0404.dart +++ b/longest-increasing-subsequence/yhkee0404.dart @@ -19,4 +19,4 @@ class Solution { }); return stack.length; } -} \ No newline at end of file +} From 98805a215b655a7473431201b9a6d8d2ce1adedb Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 28 Aug 2025 21:57:08 +0900 Subject: [PATCH 8/9] spiral matrix solution --- spiral-matrix/yhkee0404.kt | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 spiral-matrix/yhkee0404.kt diff --git a/spiral-matrix/yhkee0404.kt b/spiral-matrix/yhkee0404.kt new file mode 100644 index 000000000..6222018f3 --- /dev/null +++ b/spiral-matrix/yhkee0404.kt @@ -0,0 +1,30 @@ +class Solution { + fun spiralOrder(matrix: Array): List { + val visited = List(matrix.size) {MutableList(matrix.first().size) {false}} // T(n) = S(n) = O(n) + val drcs = listOf( + listOf(0, 1,), + listOf(1, 0,), + listOf(0, -1,), + listOf(-1, 0,), + ) + val ans = mutableListOf() + search(ans, matrix, visited, drcs, 0, 0, 0, false) + return ans + } +} + +fun search(ans: MutableList, matrix: Array, visited: List>, drcs: List>, r: Int, c: Int, dir: Int, turned: Boolean) { + val drc = drcs[dir] + if (r == -1 || r == matrix.size || c == -1 || c == matrix.first().size || visited[r][c]) { + if (turned) { + return + } + val nDir = (dir + 1) % drcs.size + val nDrc = drcs[nDir] + search(ans, matrix, visited, drcs, r - drc[0] + nDrc[0], c - drc[1] + nDrc[1], nDir, true) + return + } + visited[r][c] = true + ans.add(matrix[r][c]) + search(ans, matrix, visited, drcs, r + drc[0], c + drc[1], dir, false) +} From 1b81fc70f46ff4642ac0ab30ced2e143b0378774 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Thu, 28 Aug 2025 22:05:57 +0900 Subject: [PATCH 9/9] design add and search words data structure fixed with intermediate analyses --- design-add-and-search-words-data-structure/yhkee0404.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/design-add-and-search-words-data-structure/yhkee0404.rs b/design-add-and-search-words-data-structure/yhkee0404.rs index beb963830..636b24a87 100644 --- a/design-add-and-search-words-data-structure/yhkee0404.rs +++ b/design-add-and-search-words-data-structure/yhkee0404.rs @@ -22,7 +22,7 @@ impl WordDictionary { } } - // T(n) = S(n) = O(26n) = O(n) + // T(n) = S(n) = O(n) fn add_word(&mut self, word: String) { let mut t = &mut self.trie; for c in word.chars() { @@ -37,7 +37,7 @@ impl WordDictionary { return Self::search_from_index(&self.trie, &word) } - // T(n) = S(n) = ((26^2)n) = O(n) + // T(n) = S(n) = O((n - 2) * 26^2) = O(n) fn search_from_index(trie: &Trie, word: &str) -> bool { return match word.chars() .next() {