Skip to content
Open
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
62 changes: 62 additions & 0 deletions 349. Intersection of Two Arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
1st
それぞれの配列の要素を set に格納し、両方の set に含まれているものを結果用の set に格納すればいけそう。
時間計算量:O(n + m) num1 の長さを n、nums2 の長さを m とする
空間計算量:O(n + m)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空間計算量がO(min(n, m))の方法でできますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

nums1とnums2の長さを比較して、短い方だけをsetに格納すれば
O(min(n, m))の空間計算量で書くことができると思います。以下のような感じでしょうか。

class Solution:
    def intersection(nums1, nums2):
        if len(nums1) > len(nums2):
            nums1, nums2 = nums2, nums1

        set_nums = set(nums1)  # 小さい方の配列をセットに変換
        result = set()

        for num in nums2:
            if num in set_nums:
                result.add(num)

        return list(result)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setを一つだけ(set_numsのみ)使ってできますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

結果を返すresultをsetからリストに変えて、重複を見つけたら、set_numsからremoveするようにしました。
これ以外だと、時間計算量はO(n * m)になりますが、二重ループでそれぞれ値を取り出して値が同じならsetに格納する方法が思いつきました。

class Solution:
    def intersection(self, nums1, nums2):
        if len(nums1) > len(nums2):
            nums1, nums2 = nums2, nums1
        
        set_nums = set(nums1) 
        result = [] 

        for num in nums2:
            if num in set_nums:
                result.append(num)
                set_nums.remove(num)  

        return result

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良さそうです!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pythonは引数が値渡しなので気にしなくてもいいと思いますが、参照渡しの言語なら下のようにすると参照透過性が失われないと思いました

if len(nums1) > len(nums2):
    return self.intersection(nums2, nums1)


```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
unique_nums1 = set()
unique_nums2 = set()
intersection_result = set()

for num in nums1:
unique_nums1.add(num)

for num in nums2:
unique_nums2.add(num)

for num in unique_nums1:
if num in unique_nums2:
intersection_result.add(num)

return list(intersection_result)

```

2nd
set について調べていると、
`intersection()`というメソッドがあったのでこれも使える。

https://docs.python.org/ja/3/library/stdtypes.html#frozenset.intersection

```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
num1_set = set(nums1)
num2_set = set(nums2)
return list(num1_set.intersection(num2_set))
```

&でも積集合を求めることができるらしい。
気になったので、ここの実装を調べてみた。
& 演算子は、Python の set クラスでオーバーロードされており、**and** メソッドを呼び出していた。

https://github.com/python/cpython/blob/bd4be5e67de5f31e9336ba0fdcd545e88d70b954/Lib/_collections_abc.py#L60

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

3rd
&演算子が一番完結、かつ、bit 演算の&と同じで積を取っていることがわかりやすいので、こちらを採用
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他、両方を sort するという手もあるでしょう。
そこから、merge sort ようなことをする作戦や、片方の値でバイナリサーチをする作戦などがあるでしょう。

あと、他の人のコードを読むほうが練習として大事なので、Discord サーバー内を漁るといいと思います。


```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1_set = set(nums1)
nums2_set = set(nums2)
return list(nums1_set & nums2_set)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みですが、それぞれの set を変数化する必要もないのかなと思いました。そのぐらい読みやすいです。

```