460. LFU Cache#67
Open
kitano-kazuki wants to merge 1 commit into
Open
Conversation
nodchip
reviewed
May 26, 2026
| self.size = 0 | ||
| self.key_to_frequency = {} | ||
| self.key_to_value = {} | ||
| self.frequency_to_key_chunks = collections.OrderedDict() |
There was a problem hiding this comment.
chunk は塊を表す単語なのですが、あまり意味がないように感じました。 frequency_to_keys のほうが読み手にとって理解しやすいように感じました。
| del key_chunks[key] | ||
|
|
||
| # frequency回使用されたキー集合の中身がなくなった | ||
| if len(key_chunks) == 0: |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
mamo3gr/arai60#6 (comment)
| @@ -1 +1,112 @@ | |||
| # Step1 | |||
|
|
|||
There was a problem hiding this comment.
自分でも書いてみました。かなり面倒でした。
C++ には OrderedDict がないため、 Python に比べてひと手間掛かっています。
class LFUCache {
public:
LFUCache(int capacity) {
capacity_ = capacity;
size_ = 0;
}
int get(int key) {
if (!key_to_value_.contains(key)) {
return -1;
}
increment_frequency(key);
return key_to_value_[key];
}
void put(int key, int value) {
if (key_to_value_.contains(key)) {
increment_frequency(key);
key_to_value_[key] = value;
return;
}
++size_;
if (size_ > capacity_) {
--size_;
int frequency = frequencies_.front();
int old_key = frequency_to_keys_[frequency].front();
key_to_frequency_.erase(old_key);
key_to_value_.erase(old_key);
key_to_keys_iterator_.erase(old_key);
frequency_to_keys_[frequency].erase(frequency_to_keys_[frequency].begin());
if (frequency_to_keys_[frequency].empty()) {
frequency_to_frequencies_iterator_.erase(frequency);
frequencies_.erase(frequencies_.begin());
}
}
key_to_value_[key] = value;
if (frequencies_.empty() || frequencies_.front() != 1) {
auto it = frequencies_.insert(frequencies_.begin(), 1);
frequency_to_frequencies_iterator_[1] = it;
}
auto it = frequency_to_keys_[1].insert(frequency_to_keys_[1].end(), key);
key_to_keys_iterator_[key] = it;
key_to_frequency_[key] = 1;
}
void increment_frequency(int key) {
int old_frequency = key_to_frequency_[key];
int new_frequency = old_frequency + 1;
key_to_frequency_[key] = new_frequency;
auto it = frequency_to_frequencies_iterator_[old_frequency];
auto new_it = it;
++new_it;
if (new_it == frequencies_.end() ||
*new_it != new_frequency) {
new_it = frequencies_.insert(new_it, new_frequency);
}
frequency_to_frequencies_iterator_[new_frequency] = new_it;
frequency_to_keys_[old_frequency].erase(key_to_keys_iterator_[key]);
if (frequency_to_keys_[old_frequency].empty()) {
frequencies_.erase(frequency_to_frequencies_iterator_[old_frequency]);
frequency_to_frequencies_iterator_.erase(old_frequency);
frequency_to_keys_.erase(old_frequency);
}
frequency_to_keys_[new_frequency].push_back(key);
key_to_keys_iterator_[key] = --frequency_to_keys_[new_frequency].end();
}
int capacity_;
int size_;
list<int> frequencies_;
unordered_map<int, list<int>::iterator> frequency_to_frequencies_iterator_;
unordered_map<int, list<int>> frequency_to_keys_;
unordered_map<int, int> key_to_frequency_;
unordered_map<int, int> key_to_value_;
unordered_map<int, list<int>::iterator> key_to_keys_iterator_;
};
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.
https://leetcode.com/problems/lfu-cache/description/