-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/276. Paint Fence #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
全体的に読みやすかったです。細かい点のみコメントしています。
self.remove(key) | ||
self.add(key, node.val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get のときは、既存のキャッシュされたノードが再利用できそうです。
There was a problem hiding this comment.
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を渡す方が良いですね
if self.sentinel.previous == self.sentinel: | ||
self.sentinel.previous = node |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここはLinkedListが空のケース判定だと思いますが、おそらく不要かなと思いました。
There was a problem hiding this comment.
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] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
バケット探索・衝突解決等考えると、配列の方が若干パフォーマンスが高いかもしれないと思いました。メモリ効率も良さそうです。(そもそも直前とその前の要素のみ保持すればという話もありますが)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうなんですね。割と癖でインデックスまでの〇〇を辞書型にしてしまっている節があるので気を付けようと思います。
問題文:https://www.geeksforgeeks.org/problems/painting-the-fence3727/1
(LeetCode Premiumにつき、GeeksForGeeksで代用)