Skip to content

Conversation

Satorien
Copy link
Owner

問題文:https://www.geeksforgeeks.org/problems/painting-the-fence3727/1
(LeetCode Premiumにつき、GeeksForGeeksで代用)

Copy link

@kazukiii kazukiii left a comment

Choose a reason for hiding this comment

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

全体的に読みやすかったです。細かい点のみコメントしています。

Comment on lines +66 to +67
self.remove(key)
self.add(key, node.val)

Choose a reason for hiding this comment

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

get のときは、既存のキャッシュされたノードが再利用できそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

node.valの代わりにnodeを渡した方が良いということですね。
putでvalを入れたかったのでaddの中で作るように統一したんですが、確かに再利用できることを考えるとputでnode作ってnodeを渡す方が良いですね

Comment on lines +87 to +88
if self.sentinel.previous == self.sentinel:
self.sentinel.previous = node

Choose a reason for hiding this comment

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

ここはLinkedListが空のケース判定だと思いますが、おそらく不要かなと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

sentinel.previousにLRUのnodeを入れている実装なんですが、基本は追加するノード周りの更新作業しかしないので末尾がつながるようにするには、一つ目のnodeを入れる時につないでおく必要があると思っています。
putでキャパオーバーの時にsentinel.previousを消すときに、ここがないとsentinel.previousは初期状態のまま変更されないので上手くいかないと思います。(ちゃんと実行してなくてすみません)

```python
class Solution:
def countWays(self, n: int, k: int) -> int:
ways_to_index: dict[int, int] = {}

Choose a reason for hiding this comment

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

バケット探索・衝突解決等考えると、配列の方が若干パフォーマンスが高いかもしれないと思いました。メモリ効率も良さそうです。(そもそも直前とその前の要素のみ保持すればという話もありますが)

Copy link
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.

2 participants