-
Notifications
You must be signed in to change notification settings - Fork 0
349. Intersection of Two Arrays #14
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?
Conversation
| nums1_set = set(nums1) | ||
| nums2_set = set(nums2) | ||
| return list(nums1_set & nums2_set) |
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.
好みですが、
return list(set(nums1) & set(nums2))こうしちゃってもいいかもですね。
|
|
||
| index_nums1 = 0 | ||
| index_nums2 = 0 | ||
| answer = [] |
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.
具体的に何が入っているか分かる変数名にするのも一考かと。
問題名になっちゃいますが、intersection_of_two_arraysとか如何でしょう。
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.
ありがとうございます。
answer -> intersection とすると良さそうです。
| - https://leetcode.com/problems/get-the-maximum-score/description/ | ||
| - もともとソート済みならO(min(n, m))なので速い | ||
| - この手法でもやってみる | ||
| - この問題の場合は、ソートがボトルネックになる |
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.
確認されてるかもですが、関連ドキュメント貼っておきます。
https://docs.python.org/ja/3/howto/sorting.html#sorting-techniques
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.
ありがとうございます、一通り読みました。
Pythonのソートは3.10まではTimsortで3.11からはPowersortというアルゴリズムが使われていてどちらもmerge sortベースなんですね。
list.sort() を使っても sorted() を使っても、最悪メモリ使用量がO(n)になるというところを見落としていました(list.sort() は何かしら最適化されていてメモリ使用量O(1)でできるのかと思ってました...)
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.
in place な merge sort 存在するんですが、知らなくてもいいでしょう。
Knuth の本でも練習問題だそうです。
https://stackoverflow.com/questions/2571049/how-to-sort-in-place-using-the-merge-sort-algorithm
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.
これは知りませんでした。一通り眺めておきます。
問題へのリンク
https://leetcode.com/problems/intersection-of-two-arrays/description/
次に解く問題
929. Unique Email Addresses
README.mdへ頭の中の言語化と記録をしています。