diff --git a/contains-duplicate/jahyun-dev.rs b/contains-duplicate/jahyun-dev.rs new file mode 100644 index 0000000000..c1ad4fa02a --- /dev/null +++ b/contains-duplicate/jahyun-dev.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn contains_duplicate(nums: Vec) -> bool { + let mut nx = nums; + nx.sort(); + + for i in 1..nx.len() { + if nx[i] == nx[i - 1] { + return true; + } + } + + false + } +} diff --git a/longest-consecutive-sequence/jahyun-dev.rs b/longest-consecutive-sequence/jahyun-dev.rs new file mode 100644 index 0000000000..1665395bef --- /dev/null +++ b/longest-consecutive-sequence/jahyun-dev.rs @@ -0,0 +1,29 @@ +use std::collections::HashSet; + +impl Solution { + pub fn longest_consecutive(nums: Vec) -> i32 { + let mut set: HashSet = 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; + } +} diff --git a/top-k-frequent-elements/jahyun-dev.rs b/top-k-frequent-elements/jahyun-dev.rs new file mode 100644 index 0000000000..5609382563 --- /dev/null +++ b/top-k-frequent-elements/jahyun-dev.rs @@ -0,0 +1,16 @@ +use std::collections::HashMap; + +impl Solution { + pub fn top_k_frequent(nums: Vec, k: i32) -> Vec { + let mut m: HashMap = 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() + } +} diff --git a/two-sum/jahyun-dev.rs b/two-sum/jahyun-dev.rs new file mode 100644 index 0000000000..c2053fdb22 --- /dev/null +++ b/two-sum/jahyun-dev.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; + +impl Solution { + /** + * Hashmap 사용 시 O(n) 시간 복잡도로 가능하다. + */ + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut map: HashMap = 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, target: i32) -> Vec { + 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(); + } +}