Skip to content

141. Linked List Cycle#1

Open
yus-yus wants to merge 2 commits intomainfrom
141.Linked-List-Cycle
Open

141. Linked List Cycle#1
yus-yus wants to merge 2 commits intomainfrom
141.Linked-List-Cycle

Conversation

@yus-yus
Copy link
Copy Markdown
Owner

@yus-yus yus-yus commented Jan 29, 2025

問題:141. Linked List Cycle https://leetcode.com/problems/linked-list-cycle/description/

step1終了時に試しにやってみました

問題:141. Linked List Cycle https://leetcode.com/problems/linked-list-cycle/description/

step1終了時に試しにやってみました
step3まで終了しました
@yus-yus yus-yus changed the title Add files via upload 141. Linked List Cycle 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のほうが重複するデータや順序の情報を持たないので小さい?
⇒こういったことはどのように調べたら良いのか
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

全体的に良いと思います!

この問題では具体的にどれくらいかは分かりませんが、10万要素でリストとsetを作った場合、setの方がリストより6倍くらいメモリを使ってるそうです。
https://www.tsuyukimakoto.com/blog/2019/07/16/python_list_set_array_performance/

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.

コメントありがとうございます。
そのようですね。
下のコメントでodaさんに教えていただいたように手元で試してみたところ5, 2倍程度でした。

ここでsetであることが大事でlistだと検索に時間計算量O(n)かかるが、setだとO(1)で検索できる。
今回のように順序が関係ない場合setのほうがよい。

疑問:Pythonのlistと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.

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.

コメントありがとうございます。
手元でためしてみました。
listはその要素への参照を保存しているだけなのに対して、setはハッシュ値を計算しているためにメモリを使うことが分かりました。

  • setはハッシュテーブルを保持すること。
  • このハッシュテーブルは衝突を防ぐために要素数よりも多めに確保する必要があること。
  • このテーブルはある程度要素が追加されると一致してしまう可能性が上がるのでさらに拡張される(リハッシュ)されるのでより大きくなること。
    の3つが主な原因という理解であっているでしょうか。

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.

ご確認ありがとうございます。

Copy link
Copy Markdown

@katsukii katsukii left a comment

Choose a reason for hiding this comment

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

全体的に良いと思います。

C++で解いた経験のある問題。fast, slowの二つのポインタを用意しheadからスタートする。
fastがslowに追いついたときループがある。逆に追い付くことなくslowがnullptrまでいった場合ループはないと言える。
というのが基本的な方針として重いついたのでその方針で実装してみる。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良いと思います。Discordも漁ってみるといいかもしれません。

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.

ありがとうございます。
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.

5 participants