-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #746 from bus710/week02
[bus710] Week 02
- Loading branch information
Showing
3 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
construct-binary-tree-from-preorder-and-inorder-traversal/bus710.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package hello | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
) | ||
|
||
type TreeNode struct { | ||
Val int | ||
Left *TreeNode | ||
Right *TreeNode | ||
} | ||
|
||
func buildTree(preorder []int, inorder []int) *TreeNode { | ||
if len(preorder) == 1 { | ||
return &TreeNode{ | ||
Val: preorder[0], | ||
} | ||
} | ||
|
||
center, _ := findCenterWithValue(preorder[0], inorder) | ||
|
||
head := TreeNode{ | ||
Val: inorder[center], | ||
} | ||
|
||
execute(&head, center, inorder) | ||
|
||
return &head | ||
} | ||
|
||
func findCenterWithValue(key int, inorder []int) (int, error) { | ||
for i, n := range inorder { | ||
if n == key { | ||
return i, nil | ||
} | ||
} | ||
return 0, errors.New("not found") | ||
} | ||
|
||
func findCenter(slice []int) int { | ||
c := len(slice) / 2 | ||
return c | ||
} | ||
|
||
func getSplit(center int, inorder []int, currentVal int) ([]int, []int) { | ||
if len(inorder) == 1 { | ||
if inorder[0] == currentVal { | ||
return nil, nil | ||
} | ||
return []int{inorder[0]}, nil | ||
} | ||
if len(inorder) == 2 { | ||
if inorder[0] == currentVal { | ||
return nil, []int{inorder[1]} | ||
} | ||
if inorder[1] == currentVal { | ||
return []int{inorder[0]}, nil | ||
} | ||
return []int{inorder[0]}, []int{inorder[1]} | ||
} | ||
return inorder[:center], inorder[center+1:] | ||
} | ||
|
||
func execute(currentNode *TreeNode, center int, slice []int) { | ||
left, right := getSplit(center, slice, currentNode.Val) | ||
|
||
if len(left) == 1 { | ||
currentNode.Left = &TreeNode{Val: left[0]} | ||
} else if len(left) == 2 { | ||
currentNode.Left = &TreeNode{Val: left[0]} | ||
currentNode.Left.Right = &TreeNode{Val: left[1]} | ||
} else if len(left) > 2 { | ||
lc := findCenter(left) | ||
currentNode.Left = &TreeNode{Val: left[lc]} | ||
|
||
if len(left) > 2 { | ||
execute(currentNode.Left, lc, left) | ||
} | ||
} | ||
|
||
if len(right) == 1 { | ||
currentNode.Right = &TreeNode{Val: right[0]} | ||
} else if len(right) == 2 { | ||
currentNode.Right = &TreeNode{Val: right[0]} | ||
currentNode.Right.Left = &TreeNode{Val: right[1]} | ||
} else if len(right) > 2 { | ||
rc := findCenter(right) | ||
currentNode.Right = &TreeNode{Val: right[rc]} | ||
|
||
if len(right) > 2 { | ||
execute(currentNode.Right, rc, right) | ||
} | ||
} | ||
} | ||
|
||
func print(currentNode *TreeNode) { | ||
log.Println(currentNode.Val) | ||
if currentNode.Left != nil { | ||
print(currentNode.Left) | ||
} | ||
if currentNode.Right != nil { | ||
print(currentNode.Right) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// T: O(n log n) | ||
// S: O(n) | ||
|
||
pub fn count_bits(n: i32) -> Vec<i32> { | ||
// Prepare a vector. | ||
// The first item can just be 0 | ||
let mut vec = Vec::from([0]); | ||
|
||
// Iterate as many as the given number + 1 | ||
for num in 1..n + 1 { | ||
// Get a binary string from the number (ex: 2 => 10) | ||
let num_str = format!("{num:b}"); | ||
// Count '1' from the given binary string | ||
let cnt = num_str.chars().filter(|c| *c == '1').count(); | ||
// Store the number in the vector | ||
vec.push(cnt as i32); | ||
} | ||
|
||
vec | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = count_bits(2); | ||
assert_eq!(result, Vec::from([0, 1, 1])); | ||
|
||
let result2 = count_bits(5); | ||
assert_eq!(result2, Vec::from([0, 1, 1, 2, 1, 2])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Space complexity: O(2n) - ์ฃผ์ด์ง ํ ๋จ์ด์์ ๊ฐ ๋ฌธ์๊ฐ ์๋ก ๋ค ๋ค๋ฅผ ๊ฒฝ์ฐ ์์ฑํ ๋งต๋ค์ ์ต๋ ๊ธธ์ด๋ ์ฃผ์ด์ง ๋จ์ด๋งํผ์ด๋ฏ๋ก 2n | ||
// Time complexity: O(3n) - ๊ฐ ๋งต์ ์์ฑํ๊ณ ์ถ๊ฐ๋ก ๊ฐ ์์ดํ ์ ๋น๊ตํ๋ ๋ฃจํ๊ฐ ํ์ํ๋ฏ๋ก 3n | ||
|
||
use std::collections::HashMap; | ||
|
||
pub fn is_anagram(s: String, t: String) -> bool { | ||
// Check if the lengh of the 2 words are same. | ||
// Otherwise return false. | ||
let len01 = s.len(); | ||
let len02 = t.len(); | ||
if len01 != len02 { | ||
return false; | ||
} | ||
|
||
// Prepare and fill a new map for s | ||
let mut s_map = HashMap::new(); | ||
for s_char in s.chars() { | ||
let n = s_map.get(&s_char).copied().unwrap_or(0); | ||
s_map.insert(s_char, n + 1); | ||
} | ||
|
||
// Prepare and fill a new map for t | ||
let mut t_map = HashMap::new(); | ||
for t_char in t.chars() { | ||
let n = t_map.get(&t_char).copied().unwrap_or(0); | ||
t_map.insert(t_char, n + 1); | ||
} | ||
|
||
// Iterate over the map s, so compare with the map t | ||
// to see if both have same number for the same character respectively | ||
for (s_char, num) in s_map.iter() { | ||
if t_map.get(s_char).copied().unwrap_or(0) != *num { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_good() { | ||
let result = is_anagram("ana".to_owned(), "aan".to_owned()); | ||
assert!(result); | ||
} | ||
|
||
#[test] | ||
fn test_bad() { | ||
let result = is_anagram("aaa".to_owned(), "aan".to_owned()); | ||
assert!(!result); | ||
} | ||
} | ||
|