Skip to content

Commit 6511993

Browse files
add implementations for smallest range in k sorted lists and LRU cache
1 parent 547d33e commit 6511993

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

24binarytree/03elementsAtNthLevel.exe

0 Bytes
Binary file not shown.

26Heap/LC632.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<bits/stdc++.h>
2+
/*
3+
for k lists each of size n
4+
TC = O(nk log k)
5+
*/
6+
using namespace std;
7+
typedef pair<int, pair<int,int>> pp;
8+
//{element ,{row, col}}
9+
vector<int> smallestRange(vector<vector<int>>& nums) {
10+
priority_queue<pp, vector<pp> , greater<pp>> pq;
11+
int k = nums.size();//to make sure heap doesnot have more than 1 ele from each subarray
12+
int mx = INT_MIN;
13+
for(int i=0; i<nums.size(); i++){
14+
mx = max(mx, nums[i][0]);
15+
pq.push({nums[i][0], {i, 0}});
16+
}
17+
int min = pq.top().first;
18+
int start = min;
19+
int end = mx;
20+
while(true){
21+
int top_ele_row = pq.top().second.first;
22+
int top_ele_col = pq.top().second.second;
23+
pq.pop();
24+
if(top_ele_col == nums[top_ele_row].size()-1) break; //reaches end of a row
25+
int newele = nums[top_ele_row][top_ele_col+1];
26+
pq.push({newele, {top_ele_row,top_ele_col+1}});
27+
mx = max(mx, newele);
28+
min = pq.top().first;
29+
//comapre with previous range ans see if it is new range is less , replace start and end
30+
if(mx-min < end-start){
31+
end = mx;
32+
start = min;
33+
}
34+
}
35+
return {start, end};
36+
}
37+
int main(){
38+
vector<vector<int>> v = {{4,10,15,24,26},{0,9,12,20},{5,18,22,30}};
39+
vector<int> ans = smallestRange(v);
40+
for(int ele:ans){
41+
cout<<ele<<" ";
42+
}
43+
return 0;
44+
}

26Heap/LC632.exe

212 KB
Binary file not shown.

project/lru.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node{
5+
string val;
6+
Node* left = nullptr;
7+
Node* right = nullptr;
8+
};
9+
struct Queue{
10+
Node* head = nullptr;
11+
Node* tail = nullptr;
12+
int length;
13+
void display(){
14+
Node* n = head->right;
15+
cout<<"[";
16+
while(n!= tail){
17+
cout<<n->val<<" -> ";
18+
n = n->right;
19+
}
20+
cout<<"]"<<"\n\n";
21+
}
22+
};
23+
typedef unordered_map<string, Node*> Hash;
24+
25+
struct Cache{
26+
Queue q;
27+
Hash h;
28+
void check(string word){
29+
//find word in map if it is there remove it and insert at front of queue
30+
if(h.find(word) != h.end()){
31+
//get the node from hash
32+
Node* n = h[word];
33+
remove(n);
34+
//add code
35+
add(n);
36+
}else{
37+
Node* n = new Node({word});
38+
add(n);
39+
}
40+
}
41+
void add(Node* n){
42+
string word = n->val;
43+
cout<<"Inserting `"<<word<<"` to lru cache"<<endl;
44+
n->right = q.head->right;
45+
n->left = q.head;
46+
q.head->right = n;
47+
n->right->left = n;
48+
q.length++;
49+
h.insert({word, n});
50+
if(q.length > 5){
51+
cout<<"Cache full removing last element !!!"<<endl;
52+
remove(q.tail->left);
53+
}
54+
}
55+
Node* remove(Node* n){
56+
Node* l = n->left;
57+
Node* r = n->right;
58+
//remove
59+
l->right = r;
60+
r->left = l;
61+
q.length--;
62+
h.erase(n->val);
63+
return n;
64+
}
65+
void display(){
66+
q.display();
67+
}
68+
};
69+
Queue newQueue(){
70+
Node* head = new Node();
71+
Node* tail = new Node();
72+
tail->left = head;
73+
head->right = tail;
74+
return Queue({head, tail, 0});
75+
}
76+
Cache newCache(){
77+
Queue q = newQueue();
78+
Hash h;
79+
return Cache({q, h});
80+
}
81+
82+
int main(){
83+
Cache cache = newCache();
84+
vector<string> v = {"parrot", "avocado", "tree", "potato", "tomato", "tree", "man", "monkey", "deer", "man"};
85+
for(string s: v){
86+
cache.check(s);
87+
cache.display();
88+
}
89+
return 0;
90+
}

project/lru.exe

305 KB
Binary file not shown.

sample.exe

105 KB
Binary file not shown.

0 commit comments

Comments
 (0)