-
Notifications
You must be signed in to change notification settings - Fork 0
Add 146. LRU Cache.md #16
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
self.dummy_head = ListNode() | ||
self.dummy_tail = ListNode() |
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.
この2つ同じノードにして輪っかでもいいですね。
self.access_history.append(key) | ||
self.cache[key] = value | ||
else: | ||
self.access_history.append(key) | ||
self.cache[key] = value |
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.
L105-L127辺りにも書いたんですが、こういうのって基本はまとめるもんなんでしょうか?
まとめてしまうと、視線が上下に飛ぶ感覚があるんですが、コードの読み方が悪いんでしょうか?
if Condition1:
Statement1
elif Condition2:
Statement2
Statement3
みたいな時って、自分は「if, elifだからStatement3が共通処理だな」「Statement1見た後にStatement3みよう」「その次にStatement2」みたいな感じなんですが、先にStatement3理解してから前処理見ようっていう感覚なんでしょうか?
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.
あと、「消す」メソッドがあってもいいかもしれません。
(データ構造を整合性を保って触る最小単位が何になっているかを考えると、そういう関数があってもいいでしょう。そういうものだけを使って動かしていると、何らかの意味で各関数が整合性を保って全体が動いていることが分かりやすくなります。)
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.value = value | ||
elif len(self.key_to_node) == self.capacity: | ||
oldest_node = self._popleft() | ||
del self.key_to_node[oldest_node.key] |
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.
この行、_popleft にいれてもいいかもしれませんね。
https://leetcode.com/problems/lru-cache/description/