Skip to content

349.intersections of two arrays#13

Open
nicah4o wants to merge 1 commit into
mainfrom
349,intersections-of-two-arrays
Open

349.intersections of two arrays#13
nicah4o wants to merge 1 commit into
mainfrom
349,intersections-of-two-arrays

Conversation

@nicah4o
Copy link
Copy Markdown
Owner

@nicah4o nicah4o commented May 15, 2026

class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> num1s;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これはどういう意図の変数名でしょうか?nums1num1sで読み手は混乱しそうです。

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とほぼ同じ変数名にすることで中身の説明をするという意図です。
nums1_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.

nums1とほぼ同じ変数名にすることで中身の説明をするという意図です。

これはよく分かりませんでした。

nums1_setで良いと思います。

Comment on lines +26 to +28
for (int num : nums1) {
num1s.insert(num);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unordered_setのコンストラクタで直接構築すると良さそうです。

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.

ありがとうございます。なるほど

unordered_set<int> nums1_set(nums1.begin(), nums1.end());

という記法があるのですね。知りませんでした

int target = nums2[i];
int left = -1;
int right = nums1.size();
bool found = false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

使用していなさそうです。

最初に考えるのは全件走査することで、二重のforループでO(NM)の時間計算量になるが、constrainsより
> 1 <= nums1.length, nums2.length <= 1000

なので最大でもNM=1000000でたぶん行ける。前に10000×10000の全件走査はMLEが先に失敗したので、TLEはもう少しいけるという考え。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

実行時間を見つもることが大事だと思います。
Yuto729/leetcode#16 (comment)

実行時間を見積もってからleetcodeのMLEやTLEについて考えるのがよいと思います。

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.

レビューありがとうございます。
O(100万)のとき、cppだと10ms~1msに収まるということですね。
実行時間が1sや100msだとTLEに近いという認識でよろしいでしょうか。ご意見をお聞かせください。

Copy link
Copy Markdown

@h-masder h-masder May 15, 2026

Choose a reason for hiding this comment

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

O(100万)のとき、cppだと10ms~1msに収まるということですね。

はい、そんなイメージです。

誤解のないようにしっかり書いたほうが良いと思います。

  • O(100万)ではなく100万ステップですね。
  • また、「cppだと」というより、「cppの1 秒あたりに処理可能なおおよその計算ステップ数を1憶~10憶とすると」ですね。(もしかしたら、この仮定がどこから来たのか聞かれるかもしれません。そのときは、リンク先にあるような理由が挙がるのだと思います)。

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
Owner Author

Choose a reason for hiding this comment

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

@h-masder @liquo-rice
ご返信ありがとうございます。
100万ステップとO(100万)は違うのですね。確認してみます。

};
```
うーん結局どちらもソートしてしまうことになった。
計算量がいくつになるのか分からない。最初のソートでO(NlogN+MlogM)で以降のバイナリサーチでO(MlogN)だと思う。
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
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
特に後半についてなのですが、前問でliquo-riceさんにも指摘していただきましたが、クイックソートの計算量を考えるときにlogNとNの積のそれぞれが何を指すのかが分かっておりません。
今回の場合、nums2のそれぞれの要素について、その要素をnums1の要素数Nの中でバイナリサーチするという認識でいます。バイナリサーチは要素数を一度に二分の一に減らすので検索にかかる時間はlogNになります。

Copy link
Copy Markdown

@h-masder h-masder May 15, 2026

Choose a reason for hiding this comment

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

ありがとうございます。
バイナリーサーチのほうはそれでよいと思います。
ソートのほうを推測すると、
・ソートの計算量は、sortはクイックソートが使われていると仮定した。クイックソートの計算量はO(NlogN)とO(MlogM)といわれていることを知っているが、中身はわからない。
ということですね。

なんとなくコメントしたかったことが見えてきました。ありがとうございます。

Copy link
Copy Markdown

@h-masder h-masder May 15, 2026

Choose a reason for hiding this comment

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

ここでコメントしたかったのは、わからないことはわからないでいいので丁寧に説明したほうが良い、ということですかね。

計算量がいくつになるのか分からない。最初のソートでO(NlogN+MlogM)で以降のバイナリサーチでO(MlogN)だと思う。

これですが、「よくわからないが、なんとなくこれくらいの計算量だと思います」という意見に見えます。それはちょっと避けたいですね。
理解してから発言してくださいということではなくて、どういう理由から導かれたものであるか示してほしいということです。別にわからなくてもいいのです。バリエーションはいろいろあると思います。

・cpp referenceを見ると、sortは、ふつう、Introsortが使われることが分かった。Introsortとは〇〇のように処理をするソートであるため、計算量はO(NlogN)である。

・cpp referenceを見ると、sortは、ふつう、Introsortが使われることが分かった。Introsortについては現状動作を理解できていない。cpp referenceには、その計算量はO(NlogN)であると記載されており、ひとまずそれを信用することにする。

sortに何が使われているか、調べたが見つけることができなかった。ただ、いろいろ調べてみると、〇〇というサイトや××という本には、ソートには一般的にクイックソートが使われていると書かれている。そこでクイックソートが使われているとして計算量をO(NlogN)とした。

こんな感じで書くと、読み手もある程度納得できると思います。そしてアドバイスもできると思います。
一つ目の意見に対して「はい、そうですね」で済むかもしれません。
二つ目は、Introsortに対する議論が始まるかもしれません。
三つ目は、調べ方について教えることになるかもしれません。

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.

なるほど、ありがとうございます。
分からないことをそれはそれとして説明するということですね。心理的に回避しがちだったかもしれません。
仰っていただいた三つの類型を念頭に書こうと思います。

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.

3 participants