Skip to content

349. Intersection of Two Arrays - #7

Open
tsadamor wants to merge 1 commit into
mainfrom
0349-intersection-of-two-arrays
Open

349. Intersection of Two Arrays#7
tsadamor wants to merge 1 commit into
mainfrom
0349-intersection-of-two-arrays

Conversation

@tsadamor

@tsadamor tsadamor commented Jul 27, 2026

Copy link
Copy Markdown
Owner


return res
```
- Leetcodeのruntimeは0msになっていて、ほんとうにこれ信用できない代物だなあという感を新たにする。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCode の実行時間、確かにブレが激しいですよね。
試しに手で計算してみるといいかもしれません。
Yuto729/leetcode#16 (comment)
kazizi55/coding-challenges#10 (comment)

res = []

for num in nums1:
num_to_seen[num] = 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_to_seen と聞くと、すでにみた数字かどうかという bool が value に入るように感じるので、個人的にはnum_to_seen[num] = Trueにするのが自然に思えました。
また、実際は nums1 の頻度を格納しているので、num1_to_seenとする方が親切かもしれません。
今のまま頻度を格納したいなら、num_to_frequency とする方が実態に即しているかもです。

class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
num_to_seen = {}
res = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_to_frequency に変えて frequency が2以上の num の配列を返すようにすれば res なしでも解けると思います。


### コメント集
- https://github.com/quinn-sasha/leetcode/pull/13#discussion_r1960884543
> この問題は問題文自体では終わっていなくて、解けた後に、いくつか追加の条件が出てきて、その下でのアルゴリズムとそれらの pros and cons が要求されると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

もうご覧になっているかもしれませんが、binary search を使う解法もあります。
katataku/leetcode#12 (comment)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

*.py の内容が memo.md と重複しているので手元で実行したログを残しておきたいなどの理由がない限りは memo.md に統一しても良いのではと思いました。

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.

2 participants