-
-
Notifications
You must be signed in to change notification settings - Fork 358
[jahyun-dev] WEEK 01 Solutions #2643
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,14 @@ | ||
| impl Solution { | ||
|
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. 안녕하세요! 저는 Rust를 처음봐서, GPT를 활용해서 Python으로 번역된 코드를 기준으로 리뷰하겠습니다! sorting 알고리즘은 시간 복잡도가 O(N log N)으로 알고 있습니다. |
||
| pub fn contains_duplicate(nums: Vec<i32>) -> bool { | ||
| let mut nx = nums; | ||
| nx.sort(); | ||
|
|
||
| for i in 1..nx.len() { | ||
| if nx[i] == nx[i - 1] { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| false | ||
| } | ||
|
Comment on lines
+2
to
+13
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. @jahyun-dev 안녕하세요. 바로 아래 PR에 살고있는 스터디원입니다 :) 러스트로 풀어주셨네요! 요즘 러스트가 대세인가봐요? 많이들 보여서 신기합니다~ 추가로, 원래 자신의 밑에 있는 사람의 PR을 리뷰해주는게 룰로 알고 있어서 제 PR에 일단은 임의로 리뷰어로 지정해드렸는데 한번 놀려오셔서 보시고 코멘트 남겨주시거나, 없으시면 승인 해주시면 감사하겠습니다. 🙏 |
||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 집합에 모든 원소를 넣는 데 O(n), 각 원소에 대해 연속 구간을 찾는 과정이 집합 조회로 O(1) 이므로 전체 시간 복잡도는 O(n)입니다. 공간은 집합 저장을 위해 O(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. 특정 수보다 1 작은 값이 없는 것으로 연속된 값 중에 최솟값을 찾으신 후, 연속된 길이를 측정하셨군요! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| use std::collections::HashSet; | ||
|
|
||
| impl Solution { | ||
| pub fn longest_consecutive(nums: Vec<i32>) -> i32 { | ||
| let mut set: HashSet<i32> = HashSet::new(); | ||
| let mut max_seq: i32 = 0; | ||
|
|
||
| for &n in nums.iter() { | ||
| set.insert(n); | ||
| } | ||
|
|
||
| for &n in set.iter() { | ||
| // 시작점 | ||
| if !set.contains(&(n - 1)) { | ||
| let mut j = n + 1; | ||
| while set.contains(&j) { | ||
| j += 1; | ||
| } | ||
|
|
||
| let seq = j - n; | ||
| if seq > max_seq { | ||
| max_seq = seq | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return max_seq; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빈도수 계산은 O(n), 정렬은 O(n log n), 결과 추출은 O(k)입니다. 전체 시간 복잡도는 정렬에 의해 O(n log n)입니다. 공간은 HashMap과 결과 저장을 위해 O(n)입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| impl Solution { | ||
| pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> { | ||
|
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. @jahyun-dev 정렬로 간단하게 풀어주셨네요! 요 문제는 O(nlogn)보다 더 빠르게 푸는 follow up도 있어서 해당 방법도 한번 보고 넘어가시면 도움이 되실 것 같아서 남겨봅니다. |
||
| let mut m: HashMap<i32, i32> = HashMap::new(); | ||
|
|
||
| for n in nums { | ||
| *m.entry(n).or_insert(0) += 1; | ||
| } | ||
|
|
||
| let mut items: Vec<(i32, i32)> = m.into_iter().collect(); | ||
| items.sort_by(|a, b| b.1.cmp(&a.1)); | ||
|
|
||
| items.into_iter().take(k as usize).map(|(n, c)| n).collect() | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 각 원소에 대해 해시맵 조회와 삽입이 O(1) 이므로 전체 시간 복잡도는 O(n). 공간은 해시맵 저장을 위해 O(n)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.two_sum_a — Time: O(n^2) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(n^2) |
| Space | O(1) |
피드백: 이중 루프로 모든 쌍을 탐색하므로 시간 복잡도는 O(n^2). 공간은 상수입니다.
개선 제안: 이중 루프 대신 해시맵을 사용하는 방법으로 개선하는 것을 고려할 수 있습니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| impl Solution { | ||
| /** | ||
| * Hashmap 사용 시 O(n) 시간 복잡도로 가능하다. | ||
| */ | ||
| pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { | ||
| let mut map: HashMap<i32, i32> = HashMap::new(); | ||
|
|
||
| for i in 0..nums.len() { | ||
| let v = nums[i]; | ||
| let r = target - v; | ||
|
Comment on lines
+11
to
+12
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. @jahyun-dev v와 r이 어떤 의미인지 변수명에서 드러나면 좀 더 좋지 않을까 합니다. |
||
|
|
||
| if let Some(&j) = map.get(&r) { | ||
| return vec![i as i32, j as i32]; | ||
| } | ||
|
|
||
| map.insert(v, i as i32); | ||
| } | ||
|
|
||
| return vec![0, 0]; | ||
| } | ||
|
|
||
| pub fn two_sum_a(nums: Vec<i32>, target: i32) -> Vec<i32> { | ||
| let mut idx = [0i32, 2]; | ||
|
|
||
| for i in 0..nums.len() { | ||
| for j in (i + 1)..nums.len() { | ||
| if (nums[i] + nums[j]) == target { | ||
| return vec![i as i32, j as i32]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return idx.to_vec(); | ||
| } | ||
| } | ||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬을 위해 O(n log n) 시간 복잡도가 발생하며, 이후 인접 원소 비교는 O(n)입니다. 공간은 정렬을 위한 제자리 정렬이므로 추가 공간이 거의 필요 없습니다.
개선 제안: 현재 구현이 적절해 보입니다.