We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!! Questions you might like: ✅✅✅[ Tree Data Structure ] : th-cam.com/play/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls.html ✅✅✅[ Graphs Data Structure ] : th-cam.com/play/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C.html ✅✅✅[ January Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn.html ✅✅✅[ December Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA.html ✅✅✅[ November Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e.html ✅✅✅[ August Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe.html ✅✅✅July Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC-.html ✅✅✅June Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG.html ✅✅✅May Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e.html ✅✅✅Cracking the Coding Interview - Unique String: th-cam.com/play/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE.html Struggling in a question?? Leave in a comment and we will make a video!!!🙂🙂🙂
Have done the same approach with c++, but it's giving TLE. Can't find any single solution in c++ without tle in leetcode :( C++ Code: class Node{ public: int key, val; Node *prev, *next; Node(int key,int val){ this->key=key; this->val=val; } }; class LRUCache { public: int cap; Node* head=new Node(0,0); Node* tail=new Node(0,0); unordered_map mp;
@5:40 You remove tail.prev. I thought you would remove tail because the tail of linked list is the last node, isn't tail.prev the second last node?? Why is this not the case?
tail is the dummy node that is at end of linkedlist , similar to head , which is also dummy at start of linkedlist . we have used it to simplify the operations , and get access to last(using tail) and first(using head) node in O(1).
We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!!
Questions you might like:
✅✅✅[ Tree Data Structure ] : th-cam.com/play/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls.html
✅✅✅[ Graphs Data Structure ] : th-cam.com/play/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C.html
✅✅✅[ January Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn.html
✅✅✅[ December Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA.html
✅✅✅[ November Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e.html
✅✅✅[ August Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe.html
✅✅✅July Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC-.html
✅✅✅June Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG.html
✅✅✅May Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e.html
✅✅✅Cracking the Coding Interview - Unique String: th-cam.com/play/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE.html
Struggling in a question??
Leave in a comment and we will make a video!!!🙂🙂🙂
Under rated channel, Thanks bro, understood it
Thank you!!
Do not forget to subscribe to the channel!! 😊😊
one of the best solution...
Thank you!!
map.remove(node), how it will remove ,here key should be given not the value.
*insert() method without declaring headNext*
void insert(Node node){
//insert into map
map.put(node.key,node);
//point node's next to head's next
node.next = head.next;
//point head's next' prev to node
head.next.prev = node;
head.next=node;
node.prev=head;
}
Great explanation !
Have done the same approach with c++, but it's giving TLE. Can't find any single solution in c++ without tle in leetcode :(
C++ Code:
class Node{
public:
int key, val;
Node *prev, *next;
Node(int key,int val){
this->key=key;
this->val=val;
}
};
class LRUCache {
public:
int cap;
Node* head=new Node(0,0);
Node* tail=new Node(0,0);
unordered_map mp;
LRUCache(int capacity) {
cap=capacity;
head->next=tail;
tail->prev=head;
}
void Add(Node* newNode){
mp[newNode->key]=newNode;
Node* tmp=head->next;
head->next=newNode;
newNode->prev=head;
newNode->next=tmp;
tmp->prev=newNode;
}
void Delete(Node* node){
mp.erase(node->key);
node->prev->next=node->next;
node->next->prev=node->prev;
}
int get(int key) {
if(mp.find(key)!=mp.end()){ // key present
Node* resNode=mp[key];
Delete(resNode);
Add(resNode);
return resNode->val;
}
return -1;
}
void put(int key, int value) {
if(mp.find(key)!=mp.end()){ // key is present
Delete(mp[key]);
}
if(mp.size()==cap){
Delete(tail->prev);
}
Add(new Node(key,value));
}
};
@5:40 You remove tail.prev. I thought you would remove tail because the tail of linked list is the last node, isn't tail.prev the second last node?? Why is this not the case?
tail is the dummy node that is at end of linkedlist , similar to head , which is also dummy at start of linkedlist . we have used it to simplify the operations , and get access to last(using tail) and first(using head) node in O(1).
@@054_heenaahmed8 Thank you.
it would have been great had he covered this in the explaination but i had to look at the comments to find the results.
Understood :-)