LeetCode 460: LFU Cache Solution
Master LeetCode problem 460 (LFU Cache), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
460. LFU Cache
Problem Explanation
Explanation
To implement an LFU cache, we need to keep track of the frequency of each key and also maintain the order of keys based on their frequency and recent usage. We can achieve this by using a combination of data structures: a HashMap to store key-value pairs, a HashMap to store key-frequency pairs, and a TreeMap or a LinkedHashMap to maintain the order of keys based on their frequency and recent usage.
Here are the high-level steps:
- Use a HashMap to store the key-value pairs.
- Use another HashMap to store the key-frequency pairs.
- Use a TreeMap or a LinkedHashMap to maintain the order of keys based on their frequency and recent usage.
- Implement
getandputmethods to update frequencies and order accordingly.
Solution Code
import java.util.*;
class LFUCache {
private int capacity;
private Map<Integer, Integer> values;
private Map<Integer, Integer> frequencies;
private Map<Integer, LinkedHashSet<Integer>> frequencyMap;
private int minFrequency;
public LFUCache(int capacity) {
this.capacity = capacity;
values = new HashMap<>();
frequencies = new HashMap<>();
frequencyMap = new HashMap<>();
frequencyMap.put(1, new LinkedHashSet<>());
minFrequency = 0;
}
public int get(int key) {
if (!values.containsKey(key)) {
return -1;
}
int freq = frequencies.get(key);
frequencies.put(key, freq + 1);
frequencyMap.get(freq).remove(key);
if (freq == minFrequency && frequencyMap.get(freq).size() == 0) {
minFrequency++;
}
if (!frequencyMap.containsKey(freq + 1)) {
frequencyMap.put(freq + 1, new LinkedHashSet<>());
}
frequencyMap.get(freq + 1).add(key);
return values.get(key);
}
public void put(int key, int value) {
if (capacity <= 0) {
return;
}
if (values.containsKey(key)) {
values.put(key, value);
get(key);
return;
}
if (values.size() >= capacity) {
int evict = frequencyMap.get(minFrequency).iterator().next();
frequencyMap.get(minFrequency).remove(evict);
values.remove(evict);
frequencies.remove(evict);
}
values.put(key, value);
frequencies.put(key, 1);
frequencyMap.get(1).add(key);
minFrequency = 1;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 460 (LFU Cache)?
This page provides optimized solutions for LeetCode problem 460 (LFU Cache) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 460 (LFU Cache)?
The time complexity for LeetCode 460 (LFU Cache) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 460 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 460 in Java, C++, or Python.