-
Notifications
You must be signed in to change notification settings - Fork 0
solve: Intersection-of-Two-Arrays #3
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 |
|---|---|---|
|
|
@@ -13,25 +13,64 @@ | |
|
|
||
| /* | ||
| 講師陣はどのようなコメントを残すだろうか? | ||
| - | ||
| - if文周りの書き方が冗長なので、もう少しすっきり書ける方法がある | ||
|
|
||
| 他の人のコードを読んで考えたこと | ||
| - | ||
| - HashSetを利用したワンライナーの例。mapで参照を返すだけの部分は書き直せそう。 | ||
| https://leetcode.com/problems/intersection-of-two-arrays/solutions/4406232/one-liner-using-set-operations/ | ||
|
|
||
| 他の想定ユースケース | ||
| - | ||
| - なぜbinary_searchを使っているのか、HashSet自体は使っているのに、filterでの重複判定に使っている理由などよくわからなかった。 | ||
| Runtime Beats 100%であることをタイトルで示しているが、step1.rsの自身の実装でもRuntime Beats 100%となるので、この解法は選択肢に入らないなと思った。 | ||
| https://leetcode.com/problems/intersection-of-two-arrays/solutions/5678828/beats-100-in-runtime/ | ||
|
|
||
| 改善する時に考えたこと | ||
| - | ||
| - メソッド名と同名の変数名とならないようintersectedに変更 | ||
| - nums1とnums2でサイズが小さい方を全走査に利用する。もう片方はHashSetに変換する。 | ||
| Vec<i32>.contains() -> HashSet<i32>.contains()に変更することで時間計算量を改善する。 | ||
| - そもそもRustではHashSet::intersection()メソッドが利用できるので、step3.rsではこの方法で書く。 | ||
| */ | ||
|
|
||
| use std::collections::HashSet; | ||
|
|
||
| pub struct Solution {} | ||
| impl Solution {} | ||
| impl Solution { | ||
| pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> { | ||
| let linear_search_base; | ||
| let others: HashSet<i32>; | ||
| let mut intersected: HashSet<i32> = HashSet::with_capacity(nums1.len().min(nums2.len())); | ||
|
|
||
| // 入力のデータ型がi32と固定長なので、HashSet作成時の時間計算量はO(1) | ||
| if nums1.len() <= nums2.len() { | ||
| linear_search_base = nums1; | ||
|
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. 長さが短い方の配列をイテレートしたいということであれば以下のイメージで引数を入れ替えて関数を呼びなおすという書き方もいいかもしれません。 if nums1.len() < nums2.len() {
return intersection(nums2, nums1);
}
Owner
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. ありがとうございます。ご指摘いただ方針でだいぶすっきり書けるなと思いました。
といった理由からパフォーマンスなどの制約から関数呼出しをなるべく行わないといった状況でなければ大丈夫だと思いました。 impl Solution {
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
if nums1.len() > nums2.len() {
return Self::intersection(nums2, nums1);
}
let linear_search_base = &nums1;
let others: HashSet<i32> = HashSet::from_iter(nums2);
let mut intersected: HashSet<i32> = HashSet::with_capacity(nums1.len());
// ここで全走査するので、サイズが小さい方が効率が良い
for base in linear_search_base.into_iter() {
if others.contains(&base) {
intersected.insert(*base);
}
}
intersected.into_iter().collect()
}
} |
||
| others = HashSet::from_iter(nums2); | ||
| } else { | ||
| linear_search_base = nums2; | ||
| others = HashSet::from_iter(nums1); | ||
| } | ||
|
Comment on lines
+42
to
+49
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. 入力のデータ型がi32と固定長なので、単一要素のハッシュ計算はO(1)、HashSetの構築はO(n)、だと思ったんですが、どうでしょうか。
Owner
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 base in linear_search_base.into_iter() { | ||
| if others.contains(&base) { | ||
| intersected.insert(base); | ||
| } | ||
| } | ||
|
|
||
| intersected.into_iter().collect() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn step2_test() {} | ||
| fn step2_test() { | ||
| let mut result = Solution::intersection(vec![1, 2, 2, 1], vec![2, 2]); | ||
| result.sort(); | ||
| assert_eq!(result, vec![2]); | ||
|
|
||
| let mut result = Solution::intersection(vec![4, 9, 5], vec![9, 4, 9, 8, 4]); | ||
| result.sort(); | ||
| assert_eq!(result, vec![4, 9]); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||||||||||||||||||||||||||
| // Step2_1 | ||||||||||||||||||||||||||||||
| // 目的: step2で違和感を感じたif文の部分を修正する | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||
| 他の人のコードを読んで考えたこと | ||||||||||||||||||||||||||||||
| - 標準ライブラリのHashSet::intersection()メソッド(実装はhashbrownクレート)の実装を確認していたところ、 | ||||||||||||||||||||||||||||||
| if文でよりスッキリかける書き方を見つけた。 | ||||||||||||||||||||||||||||||
| https://docs.rs/hashbrown/latest/src/hashbrown/set.rs.html#799-809 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| 改善する時に考えたこと | ||||||||||||||||||||||||||||||
| - step2で書いたif分の部分が冗長で違和感を感じたので、より簡潔な形になるように書きなおす。 | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| use std::collections::HashSet; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| pub struct Solution {} | ||||||||||||||||||||||||||||||
| impl Solution { | ||||||||||||||||||||||||||||||
| pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> { | ||||||||||||||||||||||||||||||
| let mut intersected = HashSet::<_>::with_capacity(nums1.len().min(nums2.len())); | ||||||||||||||||||||||||||||||
|
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. この行後ろに回したらいかがでしょうか。min を取る必要が無くなりそうです。
Owner
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. ありがとうございます。直後の行で
Suggested change
|
||||||||||||||||||||||||||||||
| let (smaller_nums, larger_nums) = if nums1.len() <= nums2.len() { | ||||||||||||||||||||||||||||||
| (nums1, HashSet::<_>::from_iter(nums2)) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| (nums2, HashSet::<_>::from_iter(nums1)) | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for small in smaller_nums { | ||||||||||||||||||||||||||||||
| if larger_nums.contains(&small) { | ||||||||||||||||||||||||||||||
| intersected.insert(small); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| intersected.into_iter().collect() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||||||||||
| mod tests { | ||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||
| fn step2_1_test() { | ||||||||||||||||||||||||||||||
| let mut result = Solution::intersection(vec![1, 2, 2, 1], vec![2, 2]); | ||||||||||||||||||||||||||||||
| result.sort(); | ||||||||||||||||||||||||||||||
| assert_eq!(result, vec![2]); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| let mut result = Solution::intersection(vec![4, 9, 5], vec![9, 4, 9, 8, 4]); | ||||||||||||||||||||||||||||||
| result.sort(); | ||||||||||||||||||||||||||||||
| assert_eq!(result, vec![4, 9]); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
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.
これはnums1のサイズが小さくnums2が比較的大きいがソートはされているという条件のもとで解くとしたら、こういう解き方もあるねという話だと思います。
以下のコメントログも参考になりそうです。
katataku/leetcode#12 (comment)