Skip to content

Commit

Permalink
Merge pull request #746 from bus710/week02
Browse files Browse the repository at this point in the history
[bus710] Week 02
  • Loading branch information
bus710 authored Dec 21, 2024
2 parents f8a2b19 + 0b322c7 commit 82d87ff
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
105 changes: 105 additions & 0 deletions construct-binary-tree-from-preorder-and-inorder-traversal/bus710.go
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)
}
}
34 changes: 34 additions & 0 deletions counting-bits/bus710.rs
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]));
}
}
56 changes: 56 additions & 0 deletions valid-anagram/bus710.rs
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);
}
}

0 comments on commit 82d87ff

Please sign in to comment.