diff --git a/container-with-most-water/tedkimdev.rs b/container-with-most-water/tedkimdev.rs new file mode 100644 index 0000000000..c1c2561311 --- /dev/null +++ b/container-with-most-water/tedkimdev.rs @@ -0,0 +1,19 @@ +// TC: O(n) +// SC: O(1) +impl Solution { + pub fn max_area(heights: Vec) -> i32 { + let (mut left, mut right) = (0, heights.len() - 1); + let mut res = 0i32; + while left <= right { + res = res.max(((right - left) as i32) * heights[left].min(heights[right])); + + if heights[right] < heights[left] { + right -= 1; + } else { + left += 1; + } + } + + res + } +} diff --git a/design-add-and-search-words-data-structure/tedkimdev.go b/design-add-and-search-words-data-structure/tedkimdev.go new file mode 100644 index 0000000000..bd3e7907e3 --- /dev/null +++ b/design-add-and-search-words-data-structure/tedkimdev.go @@ -0,0 +1,56 @@ +type WordDictionary struct { + root *TrieNode +} + +func Constructor() WordDictionary { + return WordDictionary{root: NewTrieNode()} +} + +// TC: O(n) +// SC: O(t + n) +// t = total number of TrieNodes created so far +func (this *WordDictionary) AddWord(word string) { + cur := this.root + for _, c := range word { + if cur.Children[c-'a'] == nil { + cur.Children[c-'a'] = NewTrieNode() + } + cur = cur.Children[c-'a'] + } + cur.IsWord = true +} + +// TC: O(n) +func (this *WordDictionary) Search(word string) bool { + return this.dfs(word, 0, this.root) +} + +func (this *WordDictionary) dfs(word string, index int, root *TrieNode) bool { + cur := root + for i := index; i < len(word); i++ { + c := word[i] + if c == '.' { + for _, child := range cur.Children { + if child != nil && this.dfs(word, i+1, child) { + return true + } + } + return false + } else { + if cur.Children[c-'a'] == nil { + return false + } + cur = cur.Children[c-'a'] + } + } + return cur.IsWord +} + +type TrieNode struct { + Children [26]*TrieNode + IsWord bool +} + +func NewTrieNode() *TrieNode { + return &TrieNode{} +} diff --git a/longest-increasing-subsequence/tedkimdev.go b/longest-increasing-subsequence/tedkimdev.go new file mode 100644 index 0000000000..ba5a577f37 --- /dev/null +++ b/longest-increasing-subsequence/tedkimdev.go @@ -0,0 +1,32 @@ +// TC: O(n ^ 2) +// SC: O(n) +func lengthOfLIS(nums []int) int { + dp := make([]int, len(nums)) + + for i := range dp { + dp[i] = 1 + } + + for i := len(nums) - 2; i >= 0; i-- { + for j := i + 1; j < len(nums); j++ { + if nums[i] < nums[j] { + if dp[i] < 1+dp[j] { + dp[i] = 1 + dp[j] + } + } + } + } + + maxLength := 1 + for _, length := range dp { + maxLength = max(maxLength, length) + } + return maxLength +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} diff --git a/spiral-matrix/tedkimdev.rs b/spiral-matrix/tedkimdev.rs new file mode 100644 index 0000000000..7e77133ffb --- /dev/null +++ b/spiral-matrix/tedkimdev.rs @@ -0,0 +1,38 @@ +// TC: O(m * n) +// SC: O(m * n) +impl Solution { + pub fn spiral_order(matrix: Vec>) -> Vec { + let mut result: Vec = Vec::new(); + + let mut left = 0i32; + let mut right = matrix[0].len() as i32; + let mut top = 0i32; + let mut bottom = matrix.len() as i32; + + while left < right && top < bottom { + for i in left..right { + result.push(matrix[top as usize][i as usize]); + } + top += 1; + for i in top..bottom { + result.push(matrix[i as usize][(right - 1) as usize]); + } + right -= 1; + + if !(left < right && top < bottom) { + break; + } + + for i in (left..right).rev() { + result.push(matrix[(bottom - 1) as usize][i as usize]); + } + bottom -= 1; + for i in (top..bottom).rev() { + result.push(matrix[i as usize][left as usize]); + } + left += 1; + } + + result + } +} diff --git a/valid-parentheses/tedkimdev.rs b/valid-parentheses/tedkimdev.rs new file mode 100644 index 0000000000..13fd260cd1 --- /dev/null +++ b/valid-parentheses/tedkimdev.rs @@ -0,0 +1,33 @@ +// TC: O(n) +// SC: O(n) +impl Solution { + pub fn is_valid(s: String) -> bool { + let mut stack = Vec::new(); + + for c in s.chars() { + match c { + '{' | '(' | '[' => { + stack.push(c); + }, + '}' => { + if Some('{') != stack.pop() { + return false; + } + }, + ')' => { + if Some('(') != stack.pop() { + return false; + } + }, + ']' => { + if Some('[') != stack.pop() { + return false; + } + }, + _ => {}, + }; + } + + stack.is_empty() + } +}