-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0460-LFUCache.cs
86 lines (72 loc) · 2.46 KB
/
0460-LFUCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//-----------------------------------------------------------------------------
// Runtime: 268ms
// Memory Usage: 50.4 MB
// Link: https://leetcode.com/submissions/detail/366136625/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _0460_LFUCache
{
private readonly SortedDictionary<int, LinkedList<int[]>> frequencyList;
private readonly Dictionary<int, LinkedListNode<int[]>> map;
private readonly int capacity;
public _0460_LFUCache(int capacity)
{
this.capacity = capacity;
frequencyList = new SortedDictionary<int, LinkedList<int[]>>();
map = new Dictionary<int, LinkedListNode<int[]>>();
}
public int Get(int key)
{
if (!map.ContainsKey(key))
return -1;
else
{
Reorder(map[key]);
}
return map[key].Value[1];
}
public void Put(int key, int value)
{
if (capacity == 0) return;
if (map.ContainsKey(key))
map[key].Value[1] = value;
else
{
if (map.Count >= capacity)
{
int min = frequencyList.Keys.First();
map.Remove(frequencyList[min].Last.Value[0]);
frequencyList[min].RemoveLast();
if (frequencyList[min].Count == 0)
frequencyList.Remove(min);
}
map.Add(key, new LinkedListNode<int[]>(new int[] { key, value, -1 }));
}
Reorder(map[key]);
}
private void Reorder(LinkedListNode<int[]> node)
{
var freq = node.Value[2];
if (frequencyList.ContainsKey(freq))
{
frequencyList[freq].Remove(node);
if (frequencyList[freq].Count == 0)
frequencyList.Remove(freq);
}
freq++;
node.Value[2]++;
if (!frequencyList.ContainsKey(freq))
frequencyList.Add(freq, new LinkedList<int[]>());
frequencyList[freq].AddFirst(node);
}
}
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache obj = new LFUCache(capacity);
* int param_1 = obj.Get(key);
* obj.Put(key,value);
*/
}