-
Notifications
You must be signed in to change notification settings - Fork 0
349. Intersection of Two Arrays.md #12
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
Open
katataku
wants to merge
1
commit into
main
Choose a base branch
from
349.-Intersection-of-Two-Arrays
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
URL: https://leetcode.com/problems/intersection-of-two-arrays/description/ | ||
|
||
# Step 1 | ||
|
||
- 実装時間: 3分 | ||
- nums1の長さをn, nums2の長さをmとして、 | ||
- 時間計算量: O(n + m) | ||
- 空間計算量: O(n + m) | ||
|
||
```python | ||
class Solution: | ||
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
return list(set(nums1) & set(nums2)) | ||
``` | ||
|
||
# Step 2 | ||
|
||
- 気になったので、setのintersectionの実装を確認した。 | ||
- 「otherを順番に見ていって、自分に含まれていればresultに追加」というシンプルなアルゴリズムみたい。 | ||
- https://github.com/python/cpython/blob/39e69a7cd54d44c9061db89bb15c460d30fba7a6/Objects/setobject.c#L1354-L1355 | ||
|
||
- 参考にしたURL | ||
- https://github.com/nittoco/leetcode/pull/15 | ||
- https://github.com/fhiyo/leetcode/pull/16 | ||
- https://github.com/TORUS0818/leetcode/pull/15 | ||
- https://github.com/hayashi-ay/leetcode/pull/21 | ||
- https://github.com/Ryotaro25/leetcode_first60/pull/14 | ||
- https://github.com/Mike0121/LeetCode/pull/30 | ||
- https://github.com/kazukiii/leetcode/pull/14 | ||
- https://github.com/Yoshiki-Iwasa/Arai60/pull/12 | ||
- https://github.com/SuperHotDogCat/coding-interview/pull/33 | ||
- https://github.com/seal-azarashi/leetcode/pull/13 | ||
- https://github.com/hroc135/leetcode/pull/13 | ||
- https://github.com/tarinaihitori/leetcode/pull/13 | ||
- https://github.com/colorbox/leetcode/pull/27 | ||
|
||
- setのdocsも再度みといた | ||
- https://docs.python.org/ja/3/library/stdtypes.html#set-types-set-frozenset | ||
- frozensetがあるというのはわかったけど、どういう状況で役に立つのだろう。。。 | ||
- > 一方、frozenset 型はイミュータブルで、ハッシュ可能 です。作成後に内容を改変できないため、辞書のキーや他の集合の要素として用いることができます。 | ||
|
||
- https://wiki.python.org/moin/TimeComplexity#set | ||
- このドキュメントの存在を知らなかった。 | ||
|
||
- > (nanの挙動について)IEEE754を確認しておいてください。 | ||
- https://pystyle.info/floating-point-numbers/ | ||
- https://zehnpaard.hatenablog.com/entry/2022/05/30/084212 | ||
|
||
|
||
- `&`が機能するのはなぜか | ||
- `&`は`__and__`を呼んでいて、iterable同士の要素の共通部分を返している。 | ||
- https://github.com/SuperHotDogCat/coding-interview/pull/33/files#r1654937009 | ||
|
||
- 「setに変換したりlistに変換したりするのって意外と抵抗がある」 | ||
- この感覚は持ってなかった。 | ||
- 変換にはすべての要素を走査するコストがあるので、その点を理解する必要がある。 | ||
- https://github.com/nittoco/leetcode/pull/15#discussion_r1632066961 | ||
|
||
```python | ||
class Solution: | ||
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
return list(set(nums1) & set(nums2)) | ||
``` | ||
|
||
# Step 3 | ||
|
||
- nums1の長さをn, nums2の長さをmとして、 | ||
- 時間計算量: O(n + m) | ||
- 空間計算量: O(n + m) | ||
|
||
```python | ||
class Solution: | ||
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
return list(set(nums1) & set(nums2)) | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
これは、まあ、これでいいんですが、もう少し書き方にバリエーションがあるように思います。挙げてみますか?
たとえば、追加質問で考えられるのは、「片方がとても大きくて、片方がとても小さいときには、大きい方を set にするのは大変じゃないでしょうか、特に大きいほうが sort 済みのときにはどうしますか。」とかです。
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.
レビュー者ですが、この視点はなかったです。たしかにこのコードだとlen(nums1)が1e8とかでlen(nums2)が空リストだと無駄な計算をすることになりますね
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.
ありがとうございます!
他の解き方を選択するときの気持ち?状況?みたいなものがイメージできてなかったので、状況を例示いただけたことでイメージができてとっつきやすいです。
この状況についても考えてみました。「ソートされてるということを活用して、長い方は全部みたくないから二分探索を活用したいな」みたいなことを考えながら以下のように解いてみます。
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.
そうですね。
他、両方ソートされていてとても大きければ、マージソートの変形のように書くと思います。
要するにこの問題の推定される出題意図は条件を変えたときに案がいくつか出てくるかです。