Skip to content

Conversation

@kazukiii
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/intersection-of-two-arrays/description/

次に解く問題
929. Unique Email Addresses

README.mdへ頭の中の言語化と記録をしています。

Comment on lines +3 to +5
nums1_set = set(nums1)
nums2_set = set(nums2)
return list(nums1_set & nums2_set)

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 = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

具体的に何が入っているか分かる変数名にするのも一考かと。
問題名になっちゃいますが、intersection_of_two_arraysとか如何でしょう。

Copy link
Owner Author

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))なので速い
- この手法でもやってみる
- この問題の場合は、ソートがボトルネックになる

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

Timsort
https://en.wikipedia.org/wiki/Timsort

Copy link
Owner Author

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)でできるのかと思ってました...)

Copy link

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは知りませんでした。一通り眺めておきます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants