-
-
Notifications
You must be signed in to change notification settings - Fork 353
[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
base: main
Are you sure you want to change the base?
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 { | ||
| 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 | ||
| } | ||
| } |
|
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)입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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> { | ||
| 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; | ||
|
|
||
| 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)입니다. 공간은 정렬을 위한 제자리 정렬이므로 추가 공간이 거의 필요 없습니다.
개선 제안: 현재 구현이 적절해 보입니다.