Skip to content

460. LFU Cache#67

Open
kitano-kazuki wants to merge 1 commit into
mainfrom
460-lfu-cache
Open

460. LFU Cache#67
kitano-kazuki wants to merge 1 commit into
mainfrom
460-lfu-cache

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

Comment thread memo.md
self.size = 0
self.key_to_frequency = {}
self.key_to_value = {}
self.frequency_to_key_chunks = collections.OrderedDict()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

chunk は塊を表す単語なのですが、あまり意味がないように感じました。 frequency_to_keys のほうが読み手にとって理解しやすいように感じました。

Comment thread memo.md
del key_chunks[key]

# frequency回使用されたキー集合の中身がなくなった
if len(key_chunks) == 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
mamo3gr/arai60#6 (comment)

Comment thread memo.md
@@ -1 +1,112 @@
# Step1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分でも書いてみました。かなり面倒でした。
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_;
};

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