Skip to content

Commit 4e0bfa0

Browse files
add 460
1 parent 2b87fa1 commit 4e0bfa0

File tree

3 files changed

+67
-84
lines changed

3 files changed

+67
-84
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Your ideas/fixes/algorithms are more than welcome!
119119
|463|[Island Perimeter](https://leetcode.com/problems/island-perimeter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IslandPerimeter.java)| O(m*n)|O(1) | Easy|
120120
|462|[Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MinimumMovestoEqualArrayElementsII.java) | O(nlogn) |O(1) | Medium|
121121
|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/HammingDistance.java) | O(n) |O(1) | Easy|
122+
|460|[LFU Cache](https://leetcode.com/problems/lfu-cache/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_460.java) | O(?) |O(?) | Hard| Design
122123
|459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_459.java)| O(n)|O(n) | Easy| KMP
123124
|458|[Poor Pigs](https://leetcode.com/problems/poor-pigs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PoorPigs.java) | O(1) |O(1) | Easy| Math
124125
|456|[132 Pattern](https://leetcode.com/problems/132-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_456.java) | O(n) |O(n) | Medium| Stack

src/main/java/com/fishercoder/solutions/LFUCache.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedHashSet;
5+
6+
public class _460 {
7+
/**
8+
* Wikipedia: The simplest method to employ an LFU algorithm is to assign a counter to every
9+
* block that is loaded into the cache. Each time a reference is made to that block the counter
10+
* is increased by one. When the cache reaches capacity and has a new block waiting to be
11+
* inserted the system will search for the block with the lowest counter and remove it from the
12+
* cache.
13+
*
14+
* Policy to handle frequency ties: based on timestamp, the entries that get set into cache earlier will be evicted first.
15+
*/
16+
17+
class LFUCache {
18+
/**credit: https://discuss.leetcode.com/topic/69737/java-o-1-very-easy-solution-using-3-hashmaps-and-linkedhashset/2*/
19+
HashMap<Integer, Integer> vals;
20+
HashMap<Integer, Integer> counts;
21+
HashMap<Integer, LinkedHashSet<Integer>> lists;
22+
int cap;
23+
int min = -1;
24+
public LFUCache(int capacity) {
25+
cap = capacity;
26+
vals = new HashMap<>();
27+
counts = new HashMap<>();
28+
lists = new HashMap<>();
29+
lists.put(1, new LinkedHashSet<>());
30+
}
31+
32+
public int get(int key) {
33+
if(!vals.containsKey(key))
34+
return -1;
35+
int count = counts.get(key);
36+
counts.put(key, count+1);
37+
lists.get(count).remove(key);
38+
if(count==min && lists.get(count).size()==0)
39+
min++;
40+
if(!lists.containsKey(count+1))
41+
lists.put(count+1, new LinkedHashSet<>());
42+
lists.get(count+1).add(key);
43+
return vals.get(key);
44+
}
45+
46+
public void put(int key, int value) {
47+
if(cap<=0)
48+
return;
49+
if(vals.containsKey(key)) {
50+
vals.put(key, value);
51+
get(key);
52+
return;
53+
}
54+
if(vals.size() >= cap) {
55+
int evit = lists.get(min).iterator().next();
56+
lists.get(min).remove(evit);
57+
vals.remove(evit);
58+
}
59+
vals.put(key, value);
60+
counts.put(key, 1);
61+
min = 1;
62+
lists.get(1).add(key);
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy