Open
Conversation
問題:141. Linked List Cycle https://leetcode.com/problems/linked-list-cycle/description/ step1終了時に試しにやってみました
step3まで終了しました
X-XsleepZzz
reviewed
Jan 29, 2025
Comment on lines
+141
to
+150
| tk-hirom さんのレポジトリより | ||
| https://github.com/tk-hirom/Arai60/pull/1/commits/967ba6e39e0f6fd44a044778596f7dcd13ad0160 | ||
| setを使った実装ができるかをみているよう。 | ||
| ここでsetであることが大事でlistだと検索に時間計算量O(n)かかるが、setだとO(1)で検索できる。 | ||
| 今回のように順序が関係ない場合setのほうがよい。 | ||
|
|
||
| 疑問:Pythonのlistとsetでは今回の場合使用するメモリの量はどちらが大きいのか | ||
|
|
||
| 予想:setのほうが重複するデータや順序の情報を持たないので小さい? | ||
| ⇒こういったことはどのように調べたら良いのか |
There was a problem hiding this comment.
全体的に良いと思います!
この問題では具体的にどれくらいかは分かりませんが、10万要素でリストとsetを作った場合、setの方がリストより6倍くらいメモリを使ってるそうです。
https://www.tsuyukimakoto.com/blog/2019/07/16/python_list_set_array_performance/
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。
そのようですね。
下のコメントでodaさんに教えていただいたように手元で試してみたところ5, 2倍程度でした。
oda
reviewed
Jan 29, 2025
| ここでsetであることが大事でlistだと検索に時間計算量O(n)かかるが、setだとO(1)で検索できる。 | ||
| 今回のように順序が関係ない場合setのほうがよい。 | ||
|
|
||
| 疑問:Pythonのlistとsetでは今回の場合使用するメモリの量はどちらが大きいのか |
There was a problem hiding this comment.
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。
手元でためしてみました。
listはその要素への参照を保存しているだけなのに対して、setはハッシュ値を計算しているためにメモリを使うことが分かりました。
- setはハッシュテーブルを保持すること。
- このハッシュテーブルは衝突を防ぐために要素数よりも多めに確保する必要があること。
- このテーブルはある程度要素が追加されると一致してしまう可能性が上がるのでさらに拡張される(リハッシュ)されるのでより大きくなること。
の3つが主な原因という理解であっているでしょうか。
SanakoMeine
reviewed
Jan 29, 2025
| C++で解いた経験のある問題。fast, slowの二つのポインタを用意しheadからスタートする。 | ||
| fastがslowに追いついたときループがある。逆に追い付くことなくslowがnullptrまでいった場合ループはないと言える。 | ||
| というのが基本的な方針として重いついたのでその方針で実装してみる。 | ||
|
|
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
Discordも見てみたいと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題:141. Linked List Cycle https://leetcode.com/problems/linked-list-cycle/description/
step1終了時に試しにやってみました