Skip to content

Commit ded322b

Browse files
committed
LC imp
1 parent 02135f4 commit ded322b

File tree

3 files changed

+352
-0
lines changed

3 files changed

+352
-0
lines changed

146.LRUCache.cs

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// 146. LRU Cache
2+
3+
// Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
4+
5+
// Implement the LRUCache class:
6+
7+
// LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
8+
// int get(int key) Return the value of the key if the key exists, otherwise return -1.
9+
// void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
10+
// The functions get and put must each run in O(1) average time complexity.
11+
12+
13+
14+
// Example 1:
15+
16+
// Input
17+
// ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
18+
// [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
19+
// Output
20+
// [null, null, null, 1, null, -1, null, -1, 3, 4]
21+
22+
// Explanation
23+
// LRUCache lRUCache = new LRUCache(2);
24+
// lRUCache.put(1, 1); // cache is {1=1}
25+
// lRUCache.put(2, 2); // cache is {1=1, 2=2}
26+
// lRUCache.get(1); // return 1
27+
// lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
28+
// lRUCache.get(2); // returns -1 (not found)
29+
// lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
30+
// lRUCache.get(1); // return -1 (not found)
31+
// lRUCache.get(3); // return 3
32+
// lRUCache.get(4); // return 4
33+
34+
35+
//
36+
public class LRUCache {
37+
private readonly int _capacity;
38+
private readonly Dictionary<int, LinkedListNode<KeyValuePair<int, int>>> _cache;
39+
private readonly LinkedList<KeyValuePair<int, int>> _lru;
40+
41+
public LRUCache(int capacity)
42+
{
43+
_capacity = capacity;
44+
_cache = new();
45+
_lru = new();
46+
}
47+
48+
public int Get(int key)
49+
{
50+
if (!_cache.TryGetValue(key, out var node))
51+
{
52+
return -1;
53+
}
54+
55+
_lru.Remove(node);
56+
_cache[key] = _lru.AddFirst(node.Value);
57+
58+
return node.Value.Value;
59+
}
60+
61+
public void Put(int key, int value)
62+
{
63+
if (_cache.TryGetValue(key, out var node))
64+
{
65+
_lru.Remove(node);
66+
_cache.Remove(key);
67+
}
68+
69+
node = _lru.AddFirst(new KeyValuePair<int, int>(key, value));
70+
_cache[key] = node;
71+
72+
if (_lru.Count > _capacity)
73+
{
74+
node = _lru.Last;
75+
_lru.RemoveLast();
76+
_cache.Remove(node.Value.Key);
77+
}
78+
}
79+
}
80+
81+
//self defined DLL
82+
public class LRUCache {
83+
public class DoublyLinkList
84+
{
85+
public (int key, int value) val;
86+
public DoublyLinkList prev;
87+
public DoublyLinkList next;
88+
public DoublyLinkList((int, int) val, DoublyLinkList prev = null,
89+
DoublyLinkList next = null)
90+
{
91+
this.val = val;
92+
this.prev = prev;
93+
this.next = next;
94+
}
95+
}
96+
DoublyLinkList root;
97+
DoublyLinkList tail;
98+
Dictionary<int, DoublyLinkList> cache;
99+
int capacity;
100+
public LRUCache(int capacity)
101+
{
102+
cache = new Dictionary<int, DoublyLinkList>();
103+
this.capacity = capacity;
104+
root = tail = null;
105+
}
106+
107+
public int Get(int key)
108+
{
109+
if (cache.ContainsKey(key))
110+
{
111+
Remove(cache[key]);
112+
Add(cache[key]);
113+
return cache[key].val.value;
114+
}
115+
return -1;
116+
}
117+
118+
public void Put(int key, int value)
119+
{
120+
if (cache.ContainsKey(key))
121+
{
122+
Remove(cache[key]);
123+
cache.Remove(key);
124+
}
125+
DoublyLinkList node = new DoublyLinkList((key, value));
126+
Add(node);
127+
cache.Add(key, node);
128+
if(cache.Count == capacity)
129+
{
130+
Remove(root);
131+
cache.Remove(root.val.key);
132+
}
133+
}
134+
public void Add(DoublyLinkList node)
135+
{
136+
if (root == null)
137+
{
138+
root = node;
139+
tail = node;
140+
}
141+
tail.next = node;
142+
node.prev = tail;
143+
tail = node;
144+
}
145+
public void Remove(DoublyLinkList node)
146+
{
147+
if (root == tail)
148+
root = tail = null;
149+
else if(node == root)
150+
{
151+
root = root.next;
152+
root.prev = null;
153+
}
154+
else if(node == tail)
155+
{
156+
tail = tail.prev;
157+
tail.next = null;
158+
}
159+
else
160+
{
161+
DoublyLinkList Prev = node.prev;
162+
DoublyLinkList Next = node.next;
163+
if (Prev != null)
164+
Prev.next = Next;
165+
if (Next != null)
166+
Next.prev = Prev;
167+
}
168+
}
169+
}

155.MinStack.cs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// 155. Min Stack
2+
3+
// Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
4+
5+
// Implement the MinStack class:
6+
7+
// MinStack() initializes the stack object.
8+
// void push(int val) pushes the element val onto the stack.
9+
// void pop() removes the element on the top of the stack.
10+
// int top() gets the top element of the stack.
11+
// int getMin() retrieves the minimum element in the stack.
12+
// You must implement a solution with O(1) time complexity for each function.
13+
14+
15+
16+
// Example 1:
17+
18+
// Input
19+
// ["MinStack","push","push","push","getMin","pop","top","getMin"]
20+
// [[],[-2],[0],[-3],[],[],[],[]]
21+
22+
// Output
23+
// [null,null,null,null,-3,null,0,-2]
24+
25+
// Explanation
26+
// MinStack minStack = new MinStack();
27+
// minStack.push(-2);
28+
// minStack.push(0);
29+
// minStack.push(-3);
30+
// minStack.getMin(); // return -3
31+
// minStack.pop();
32+
// minStack.top(); // return 0
33+
// minStack.getMin(); // return -2
34+
35+
public class MinStack {
36+
Stack<(int val,int min)> stack;
37+
38+
public MinStack() {
39+
stack = new Stack<(int,int)>();
40+
}
41+
42+
public void Push(int val) {
43+
if(stack.Count == 0){
44+
stack.Push((val,val));
45+
}
46+
else{
47+
int min = stack.Peek().min;
48+
min = Math.Min(min,val);
49+
stack.Push((val,min));
50+
}
51+
}
52+
53+
public void Pop() {
54+
stack.Pop();
55+
}
56+
57+
public int Top() {
58+
return stack.Peek().val;
59+
}
60+
61+
public int GetMin() {
62+
return stack.Peek().min;
63+
}
64+
}
65+
66+
/**
67+
* Your MinStack object will be instantiated and called as such:
68+
* MinStack obj = new MinStack();
69+
* obj.Push(val);
70+
* obj.Pop();
71+
* int param_3 = obj.Top();
72+
* int param_4 = obj.GetMin();
73+
*/

23.MergeKLists.cs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// 23. Merge k Sorted Lists
2+
3+
// You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
4+
5+
// Merge all the linked-lists into one sorted linked-list and return it.
6+
7+
8+
9+
// Example 1:
10+
11+
// Input: lists = [[1,4,5],[1,3,4],[2,6]]
12+
// Output: [1,1,2,3,4,4,5,6]
13+
// Explanation: The linked-lists are:
14+
// [
15+
// 1->4->5,
16+
// 1->3->4,
17+
// 2->6
18+
// ]
19+
// merging them into one sorted list:
20+
// 1->1->2->3->4->4->5->6
21+
// Example 2:
22+
23+
// Input: lists = []
24+
// Output: []
25+
// Example 3:
26+
27+
// Input: lists = [[]]
28+
// Output: []
29+
30+
//Approch Divide & Conqure -> Merge Sort
31+
public class Solution {
32+
public ListNode MergeKLists(ListNode[] lists) {
33+
if(lists == null || lists.Length == 0)
34+
return null;
35+
return MergeSort(lists, 0, lists.Length-1);
36+
}
37+
public ListNode MergeSort(ListNode[] lists, int low, int high){
38+
if(low == high)
39+
return lists[low];
40+
int mid = (high-low)/2 + low;
41+
ListNode left = MergeSort(lists, low, mid),
42+
right = MergeSort(lists, mid+1, high);
43+
return Merge(left, right);
44+
}
45+
public ListNode Merge(ListNode l1, ListNode l2){
46+
ListNode head = new ListNode();
47+
ListNode curr = head;
48+
49+
while(l1 != null && l2 != null){
50+
if(l1.val <= l2.val){
51+
curr.next = l1;
52+
l1 = l1.next;
53+
}
54+
else{
55+
curr.next = l2;
56+
l2 = l2.next;
57+
}
58+
curr = curr.next;
59+
}
60+
if(l1 != null){
61+
curr.next = l1;
62+
}
63+
if(l2 != null){
64+
curr.next = l2;
65+
}
66+
return head.next;
67+
}
68+
}
69+
70+
//Approch MinHeap Time O(KLogN) Space O(1)
71+
public class Solution {
72+
public class ListNode
73+
{
74+
public int val;
75+
public ListNode next;
76+
public ListNode(int val = 0, ListNode next = null)
77+
{
78+
this.val = val;
79+
this.next = next;
80+
}
81+
}
82+
public ListNode MergeKLists(ListNode[] lists)
83+
{
84+
PriorityQueue<ListNode> minHeap = new PriorityQueue<ListNode>();
85+
ListNode head = null;
86+
ListNode current = null;
87+
foreach(var item in lists)
88+
{
89+
minHeap.Add(item);
90+
}
91+
while (minHeap.Count > 0)
92+
{
93+
var min = minHeap.Dequeue();
94+
var node = new ListNode(min.val);
95+
if (head == null)
96+
{
97+
current = node;
98+
head = current;
99+
}
100+
else
101+
{
102+
current.next = node;
103+
}
104+
min = min.next;
105+
if (node == null)
106+
minHeap.Dequeue(node);
107+
}
108+
return head;
109+
}
110+
}

0 commit comments

Comments
 (0)