Skip to content

349. Intersection of Two Arrays#13

Open
tarinaihitori wants to merge 1 commit into
mainfrom
349-intersection-of-the-two-array
Open

349. Intersection of Two Arrays#13
tarinaihitori wants to merge 1 commit into
mainfrom
349-intersection-of-the-two-array

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

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)

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 を変数化する必要もないのかなと思いました。そのぐらい読みやすいです。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

良いと思います

```

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 サーバー内を漁るといいと思います。

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.

6 participants