diff --git a/container-with-most-water/yhkee0404.go b/container-with-most-water/yhkee0404.go new file mode 100644 index 000000000..0dcf3471e --- /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 +} 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..636b24a87 --- /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(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) = O((n - 2) * 26^2) = 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(), + } + } +} diff --git a/longest-increasing-subsequence/yhkee0404.dart b/longest-increasing-subsequence/yhkee0404.dart new file mode 100644 index 000000000..b88c382ea --- /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; + } +} 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) +} 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 + } +}