-
-
Notifications
You must be signed in to change notification settings - Fork 339
[tedkimdev] WEEK 02 solutions #2406
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
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,52 @@ | ||
| // TC: O(n^2) | ||
| // SC: O(m) | ||
| func threeSum(nums []int) [][]int { | ||
| result := make(map[[3]int]struct{}) | ||
|
|
||
| sort.Ints(nums) | ||
|
|
||
| // target value = nums[k] | ||
| for k := 0; k < len(nums); k++ { | ||
| left := 0 | ||
| if k == left { | ||
| left = 1 | ||
| } | ||
| right := len(nums) - 1 | ||
| if k == right { | ||
| right = len(nums) - 2 | ||
| } | ||
|
|
||
| target := nums[k] | ||
| for left < right { | ||
| sum := nums[left] + nums[right] + target | ||
| if sum == 0 { | ||
| if left > k { | ||
|
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. 중복제거를 key 통일 방식으로 해결하셨군요
Contributor
Author
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. sum 계산하기 전에 key 중복 확인을 먼저하는게 효율이 좋을거 같다는 말씀이시죠?
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. 넵 맞습니다 |
||
| result[[3]int{nums[k], nums[left], nums[right]}] = struct{}{} | ||
| } else if left < k && k < right { | ||
| result[[3]int{nums[left], nums[k], nums[right]}] = struct{}{} | ||
| } else if k > right { | ||
| result[[3]int{nums[left], nums[right], nums[k]}] = struct{}{} | ||
| } | ||
| } | ||
| if sum < 0 { | ||
| left++ | ||
| if k == left { | ||
| left++ | ||
| } | ||
| } else { | ||
| right-- | ||
| if k == right { | ||
| right-- | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| filteredResult := [][]int{} | ||
| for k, _ := range result { | ||
| filteredResult = append(filteredResult, k[0:len(k)]) | ||
| } | ||
|
|
||
| return result.keys() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // TC: O(n) | ||
| // SC: O(1) | ||
| impl Solution { | ||
| pub fn climb_stairs(n: i32) -> i32 { | ||
| if n <= 2 { | ||
| return n; | ||
| } | ||
|
|
||
| let mut arr: [i32; 2] = [1, 2]; | ||
| let mut current = 0; | ||
| for n in 3..=n { | ||
|
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. 변수명이 반복되어 오해의 소지가 있는 것 같습니다!
Contributor
Author
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. 그러게요. 사용안하고 있었네요. 감사합니다. |
||
| current = arr[0] + arr[1]; | ||
| arr[0] = arr[1]; | ||
| arr[1] = current; | ||
| } | ||
| current | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // TC: O(n) | ||
| // SC: O(n) | ||
| func productExceptSelf(nums []int) []int { | ||
| result := make([]int, 0) | ||
|
|
||
| left := make([]int, len(nums)) | ||
| left[0] = 1 | ||
|
|
||
| right := make([]int, len(nums)) | ||
| right[len(nums)-1] = 1 | ||
|
|
||
| for i := 1; i < len(nums); i++ { | ||
| left[i] = nums[i-1] * left[i-1] | ||
| } | ||
|
|
||
| for i := len(nums) - 2; i >= 0; i-- { | ||
| right[i] = nums[i+1] * right[i+1] | ||
| } | ||
|
|
||
| for i := 0; i < len(nums); i++ { | ||
| result = append(result, left[i]*right[i]) | ||
| } | ||
| return result | ||
| } | ||
|
Comment on lines
+3
to
+24
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. 오! left와 right 배열을 각각 만들어서 계산하셨군요.
Contributor
Author
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. 제가 읽고 이해하기가 불편해서 for 문을 나눠버렸었네요. 리뷰 감사합니다. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // TC: O(n) - n is the length of the strings | ||
| // SC: O(1) - lowercase english letters | ||
| impl Solution { | ||
| pub fn is_anagram(s: String, t: String) -> bool { | ||
| if s.len() != t.len() { | ||
| return false; | ||
| } | ||
|
|
||
| let mut count_map: HashMap<u8, i32> = HashMap::new(); | ||
|
|
||
| for (a, b) in s.bytes().zip(t.bytes()) { | ||
| *count_map.entry(a).or_insert(0) += 1; | ||
| *count_map.entry(b).or_insert(0) -= 1; | ||
| } | ||
|
|
||
| count_map.values().all(|&v| v == 0) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Definition for a binary tree node. | ||
| // #[derive(Debug, PartialEq, Eq)] | ||
| // pub struct TreeNode { | ||
| // pub val: i32, | ||
| // pub left: Option<Rc<RefCell<TreeNode>>>, | ||
| // pub right: Option<Rc<RefCell<TreeNode>>>, | ||
| // } | ||
| // | ||
| // impl TreeNode { | ||
| // #[inline] | ||
| // pub fn new(val: i32) -> Self { | ||
| // TreeNode { | ||
| // val, | ||
| // left: None, | ||
| // right: None, | ||
| // } | ||
| // } | ||
| // } | ||
|
|
||
| use std::rc::Rc; | ||
| use std::cell::RefCell; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) - skewed tree | ||
| impl Solution { | ||
| pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { | ||
| is_valid(root, i64::MIN, i64::MAX) | ||
| } | ||
| } | ||
| fn is_valid(node: Option<Rc<RefCell<TreeNode>>>, left: i64, right: i64) -> bool { | ||
| if let Some(n) = node { | ||
| let node_ref = n.borrow(); | ||
| let val = node_ref.val as i64; | ||
|
|
||
| if val <= left || val >= right { | ||
| return false; | ||
| } | ||
| return is_valid(node_ref.left.clone(), left, val) | ||
| && is_valid(node_ref.right.clone(), val, right); | ||
| } | ||
| true | ||
| } |
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.
반본묵 문법이 신기하네요
옛~날에 배운걸 떠올려보면 반복문이 for문으로 통일되어 있던것 같은데 맞나요?
Uh oh!
There was an error while loading. Please reload this page.
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.
네 맞습니다. 이 경우는 while 문으로 보시면 될 것 같아요.