Skip to content

Commit 84dfc01

Browse files
committed
add week2 solution
1 parent b00545f commit 84dfc01

5 files changed

Lines changed: 154 additions & 0 deletions

File tree

3sum/tedkimdev.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// TC: O(n^2)
2+
// SC: O(m)
3+
func threeSum(nums []int) [][]int {
4+
result := make(map[[3]int]struct{})
5+
6+
sort.Ints(nums)
7+
8+
// target value = nums[k]
9+
for k := 0; k < len(nums); k++ {
10+
left := 0
11+
if k == left {
12+
left = 1
13+
}
14+
right := len(nums) - 1
15+
if k == right {
16+
right = len(nums) - 2
17+
}
18+
19+
target := nums[k]
20+
for left < right {
21+
sum := nums[left] + nums[right] + target
22+
if sum == 0 {
23+
if left > k {
24+
result[[3]int{nums[k], nums[left], nums[right]}] = struct{}{}
25+
} else if left < k && k < right {
26+
result[[3]int{nums[left], nums[k], nums[right]}] = struct{}{}
27+
} else if k > right {
28+
result[[3]int{nums[left], nums[right], nums[k]}] = struct{}{}
29+
}
30+
}
31+
if sum < 0 {
32+
left++
33+
if k == left {
34+
left++
35+
}
36+
} else {
37+
right--
38+
if k == right {
39+
right--
40+
}
41+
}
42+
}
43+
44+
}
45+
46+
filteredResult := [][]int{}
47+
for k, _ := range result {
48+
filteredResult = append(filteredResult, k[0:len(k)])
49+
}
50+
51+
return result.keys()
52+
}

climbing-stairs/tedkimdev.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn climb_stairs(n: i32) -> i32 {
5+
if n <= 2 {
6+
return n;
7+
}
8+
9+
let mut arr: [i32; 2] = [1, 2];
10+
let mut current = 0;
11+
for n in 3..=n {
12+
current = arr[0] + arr[1];
13+
arr[0] = arr[1];
14+
arr[1] = current;
15+
}
16+
current
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package productofarrayexceptself
2+
3+
func productExceptSelf(nums []int) []int {
4+
result := make([]int, 0)
5+
6+
left := make([]int, len(nums))
7+
left[0] = 1
8+
9+
right := make([]int, len(nums))
10+
right[len(nums)-1] = 1
11+
12+
for i := 1; i < len(nums); i++ {
13+
left[i] = nums[i-1] * left[i-1]
14+
}
15+
16+
for i := len(nums) - 2; i >= 0; i-- {
17+
right[i] = nums[i+1] * right[i+1]
18+
}
19+
20+
for i := 0; i < len(nums); i++ {
21+
result = append(result, left[i]*right[i])
22+
}
23+
return result
24+
}

valid-anagram/tedkimdev.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// TC: O(n) - n is the length of the strings
2+
// SC: O(1) - lowercase english letters
3+
impl Solution {
4+
pub fn is_anagram(s: String, t: String) -> bool {
5+
if s.len() != t.len() {
6+
return false;
7+
}
8+
9+
let mut count_map: HashMap<u8, i32> = HashMap::new();
10+
11+
for (a, b) in s.bytes().zip(t.bytes()) {
12+
*count_map.entry(a).or_insert(0) += 1;
13+
*count_map.entry(b).or_insert(0) -= 1;
14+
}
15+
16+
count_map.values().all(|&v| v == 0)
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None,
16+
// }
17+
// }
18+
// }
19+
20+
use std::rc::Rc;
21+
use std::cell::RefCell;
22+
23+
// TC: O(n)
24+
// SC: O(n) - skewed tree
25+
impl Solution {
26+
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
27+
is_valid(root, i64::MIN, i64::MAX)
28+
}
29+
}
30+
fn is_valid(node: Option<Rc<RefCell<TreeNode>>>, left: i64, right: i64) -> bool {
31+
if let Some(n) = node {
32+
let node_ref = n.borrow();
33+
let val = node_ref.val as i64;
34+
35+
if val <= left || val >= right {
36+
return false;
37+
}
38+
return is_valid(node_ref.left.clone(), left, val)
39+
&& is_valid(node_ref.right.clone(), val, right);
40+
}
41+
true
42+
}

0 commit comments

Comments
 (0)