Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions arai60/intersection_of_the_two_arrays/phase1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1 = set(nums1)
nums2 = set(nums2)
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

入力を破壊しても良いかどうかは確認した方がいいかもしれません。

Choose a reason for hiding this comment

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

すみません、Pythonの場合正確には「参照の値渡し」だったので、この書き方で問題なかったです。
このコメントは無視してください。

return list(nums1 & nums2)
36 changes: 36 additions & 0 deletions arai60/intersection_of_the_two_arrays/phase2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Reference
ryotaro: https://github.com/Ryotaro25/leetcode_first60/pull/14/files
mike: https://github.com/Mike0121/LeetCode/pull/30/files
kazukiii: https://github.com/kazukiii/leetcode/pull/14/files
本題ではないが, pythonのsortedとlist.sort()の計算中に空間計算量O(n)を消費する情報を得られてラッキー

numpy実装も確認したが,,,python側から見えるのはnp.arrayが実装されている前提のものだった
https://github.com/numpy/numpy/blob/v2.0.0/numpy/lib/_arraysetops_impl.py#L578-L668
"""

class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))

# sortを用いて
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1 = sorted(nums1)
nums2 = sorted(nums2)
intersection_of_the_two = []
seen = set()
i1 = 0
i2 = 0
while i1 < len(nums1) and i2 < len(nums2):
if nums1[i1] == nums2[i2]:
if nums1[i1] not in seen:
intersection_of_the_two.append(nums1[i1])
seen.add(nums1[i1])

Choose a reason for hiding this comment

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

最初にソートしていて同じ値のものは隣合っていることが保証されているので、ポインタをうまく移動させることで重複排除にseenを使わなくても済み、ちょっとメモリを節約できます。

具体的には以下のような感じです。

while i1 < len(nums1) and i2 < len(nums2):
    if nums1[i1] == nums2[i2]:
        common = nums1[i1]
        intersection_of_the_two.append(common)
        while i1 < len(nums1) and nums1[i1] == common:
            i1 += 1 
        while i2 < len(nums2) and nums2[i2] == common:
            i2 += 1
    elif nums1[i1] < nums2[i2]:
        i1 += 1
    else:
        i2 += 1

Copy link
Owner Author

Choose a reason for hiding this comment

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

ソートしているのでseenがなくても重複している値はわかるというのは盲点でした。とてもスッキリした考察ありがとうございます。seen書くのは空間計算量が増えるので結構ためらって書いたので助かります。

i1 += 1
i2 += 1
elif nums1[i1] < nums2[i2]:
i1 += 1
else:
i2 += 1
return intersection_of_the_two
3 changes: 3 additions & 0 deletions arai60/intersection_of_the_two_arrays/phase3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))

Choose a reason for hiding this comment

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

&intersection()と同値なことができるのってどうしてなんですか??
operator & を set classがメソッドとして持っていると推測してるのですが、ソースコード見つけれられず

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

operator & を set classがメソッドとして持っていると推測してるのですが<-これであっていると思います。コードは上のCPythonにあります

Choose a reason for hiding this comment

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

なるほど ありがとうございます
__and__って書くんですね(^^ ;) grepしても見つからないわけだ

Copy link

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

C++のような文法を予想して探してたのですぐに見つかりませんでした

Copy link

Choose a reason for hiding this comment

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

Rust は、これですかね?
https://doc.rust-lang.org/std/ops/index.html

Choose a reason for hiding this comment

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

ですです

Note that the && and || operators are currently not supported for overloading. Due to their short circuiting nature, they require a different design from traits for other operators like BitAnd. Designs for them are under discussion.

これは知らなかった。なんでなのか調べるの面白そうですね