-
-
Notifications
You must be signed in to change notification settings - Fork 341
[tedkimdev] WEEK 06 Solutions #2517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // TC: O(n) | ||
| // SC: O(1) | ||
| impl Solution { | ||
| pub fn max_area(heights: Vec<i32>) -> 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 | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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{} | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // TC: O(m * n) | ||
| // SC: O(m * n) | ||
| impl Solution { | ||
| pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> { | ||
| let mut result: Vec<i32> = 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 | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석