Skip to content

Commit

Permalink
day06dsa
Browse files Browse the repository at this point in the history
  • Loading branch information
33arsenic75 committed Jun 1, 2024
1 parent a70c452 commit 8d5a34d
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 0 deletions.
34 changes: 34 additions & 0 deletions problem-of-the-day/day05/Add Two Numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
return addnumber(l1,l2,0);
}
ListNode* addnumber(ListNode* l1, ListNode* l2,int carry){
if(!l1 && !l2){
if(carry==1){
ListNode* n =new ListNode;
n->val=carry;
return n;
}
else return nullptr;
}
if(!l1){
int val=l2->val;
val+=carry;
l2->val = val %10;
l2->next = addnumber(nullptr,l2->next,val/10);
return l2;
}
else if(!l2){
int val=l1->val;
val+=carry;
l1->val = val %10;
l1->next = addnumber(nullptr,l1->next,val/10);
return l1;
}
int val = l1->val + l2->val + carry;
l1-> val = val%10;
l1->next = addnumber(l1->next,l2->next,val/10);
return l1;
}
};
24 changes: 24 additions & 0 deletions problem-of-the-day/day05/Add Two Numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummyHead = ListNode(0)
tail = dummyHead
carry = 0

while l1 is not None or l2 is not None or carry != 0:
digit1 = l1.val if l1 is not None else 0
digit2 = l2.val if l2 is not None else 0

sum = digit1 + digit2 + carry
digit = sum % 10
carry = sum // 10

newNode = ListNode(digit)
tail.next = newNode
tail = tail.next

l1 = l1.next if l1 is not None else None
l2 = l2.next if l2 is not None else None

result = dummyHead.next
dummyHead.next = None
return result
43 changes: 43 additions & 0 deletions problem-of-the-day/day05/Design Twitter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Twitter {
public:
unordered_map<int,unordered_set<int>>ds;
//This Data Structure will store list of twitter users and corresponding to each user there will be a set consiting of userid whom the user follow;
vector<pair<int,int>>posts;
//this vector will store the list of posts ::each post will have a tweetId and
//the userId of the user Who posted it
Twitter() {

}

void postTweet(int userId, int tweetId) {

posts.push_back({tweetId,userId});
}

vector<int> getNewsFeed(int userId) {
//If current user tries to retrieve 10 most recent post then he will be able to retrieve the posts that he himself posted or the post posted by another user whom current user follow
vector<int>ans;

//Iterate from last of posts since we want most recent
int count=1;
for(int i=posts.size()-1;i>=0&&count<=10;i--){
int Post_UserId=posts[i].second;
int post_Id=posts[i].first;
if(Post_UserId==userId||ds[userId].find(Post_UserId)!=ds[userId].end()){
ans.push_back(post_Id);
count++;
}
}
return ans;
}

void follow(int followerId, int followeeId) {
//If A follows B then in a set corresponding to A insert B
ds[followerId].insert(followeeId);
}

void unfollow(int followerId, int followeeId) {
//If A unfollows B then from a set corresponding to A remove B
ds[followerId].erase(followeeId);
}
};
58 changes: 58 additions & 0 deletions problem-of-the-day/day05/Design Twitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Twitter:
def __init__(self):
self.user_tweets = {} # key = userId : value = deque of their tweets
self.user_follows = {} # key = userId : value = set of whom they follow
self.clock = 0 # even though self.user_tweets preserves order for a user, we need a clock to compare tweet times across users

def postTweet(self, userId: int, tweetId: int) -> None:
if userId not in self.user_tweets:
self.user_tweets[userId] = deque()

if userId not in self.user_follows:
self.user_follows[userId] = set([userId])

self.user_tweets[userId].append((self.clock, tweetId))
self.clock += 1
if len(self.user_tweets[userId]) > 10:
self.user_tweets[userId].popleft() # discard the oldest tweet

def getNewsFeed(self, userId: int) -> List[int]:
if userId not in self.user_follows:
self.user_follows[userId] = set([userId])

n = len(self.user_follows[userId])
index_to_pick = [-1] * n # tracks current most recent tweet
feed = []

# we want to pick 10 max elements from n sorted lists
while len(feed) < 10:
max_time = -1
max_tweet = None
max_followee = None

for i, followee in enumerate(self.user_follows[userId]):
# if possible, consider the last index (most recent tweet) of each followee
if followee in self.user_tweets and (-index_to_pick[i]) <= len(self.user_tweets[followee]):
time, tweet = self.user_tweets[followee][index_to_pick[i]]
if time > max_time:
max_time = time
max_tweet = tweet
max_followee = i

if max_followee is None: # no more tweets
break

index_to_pick[max_followee] -= 1 # move to previous index in the list for this followee
feed.append(max_tweet)

return feed

def follow(self, followerId: int, followeeId: int) -> None:
if followerId not in self.user_follows:
self.user_follows[followerId] = set([followerId])

self.user_follows[followerId].add(followeeId)

def unfollow(self, followerId: int, followeeId: int) -> None:
if followerId in self.user_follows and followeeId in self.user_follows[followerId]:
self.user_follows[followerId].remove(followeeId)
76 changes: 76 additions & 0 deletions problem-of-the-day/day05/LRU Cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class LRUCache {
public:
class Node{
public:
int key;
int val;
Node* prev;
Node* next;

Node(int key, int val){
this->key = key;
this->val = val;
}
};

Node* head = new Node(-1, -1);
Node* tail = new Node(-1, -1);

int cap;
unordered_map<int, Node*> m;

LRUCache(int capacity) {
cap = capacity;
head -> next = tail;
tail -> prev = head;
}

void addNode(Node* newnode){
Node* temp = head -> next;

newnode -> next = temp;
newnode -> prev = head;

head -> next = newnode;
temp -> prev = newnode;
}

void deleteNode(Node* delnode){
Node* prevv = delnode -> prev;
Node* nextt = delnode -> next;

prevv -> next = nextt;
nextt -> prev = prevv;
}

int get(int key) {
if(m.find(key) != m.end()){
Node* resNode = m[key];
int ans = resNode -> val;

m.erase(key);
deleteNode(resNode);
addNode(resNode);

m[key] = head -> next;
return ans;
}
return -1;
}

void put(int key, int value) {
if(m.find(key) != m.end()){
Node* curr = m[key];
m.erase(key);
deleteNode(curr);
}

if(m.size() == cap){
m.erase(tail -> prev -> key);
deleteNode(tail -> prev);
}

addNode(new Node(key, value));
m[key] = head -> next;
}
};
52 changes: 52 additions & 0 deletions problem-of-the-day/day05/LRU Cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class LRUCache:
class Node:
def __init__(self, key, val):
self.key = key
self.val = val
self.prev = None
self.next = None

def __init__(self, capacity: int):
self.cap = capacity
self.head = self.Node(-1, -1)
self.tail = self.Node(-1, -1)
self.head.next = self.tail
self.tail.prev = self.head
self.m = {}

def addNode(self, newnode):
temp = self.head.next
newnode.next = temp
newnode.prev = self.head
self.head.next = newnode
temp.prev = newnode

def deleteNode(self, delnode):
prevv = delnode.prev
nextt = delnode.next
prevv.next = nextt
nextt.prev = prevv

def get(self, key: int) -> int:
if key in self.m:
resNode = self.m[key]
ans = resNode.val
del self.m[key]
self.deleteNode(resNode)
self.addNode(resNode)
self.m[key] = self.head.next
return ans
return -1

def put(self, key: int, value: int) -> None:
if key in self.m:
curr = self.m[key]
del self.m[key]
self.deleteNode(curr)

if len(self.m) == self.cap:
del self.m[self.tail.prev.key]
self.deleteNode(self.tail.prev)

self.addNode(self.Node(key, value))
self.m[key] = self.head.next
40 changes: 40 additions & 0 deletions problem-of-the-day/day05/Merge K Sorted List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

class Solution {
public:
ListNode* mergetwo(ListNode* h1, ListNode* h2)
{
ListNode* dummy = new ListNode(-1);
ListNode* ptr = dummy;
while(h1 && h2)
{
if(h1->val < h2->val)
{
ptr->next = h1;
h1 = h1->next;
}
else
{
ptr->next = h2;
h2 = h2->next;
}
ptr = ptr->next;
}

// to check if any list is remaining, then adding the rest of the remaining list to dummy list whih is our answer
if(h1!=NULL) ptr->next = h1;
if(h2!=NULL) ptr->next = h2;
return dummy->next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.empty()) return NULL;
ListNode* temp = lists[0];
if(lists.size()>1)
{
for(int i=1;i<lists.size();i++)
{
temp = mergetwo(temp,lists[i]);
}
}
return temp;
}
};
34 changes: 34 additions & 0 deletions problem-of-the-day/day05/Merge K Sorted List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution(object):
def mergeKLists(self, lists):
# Base cases: if the input list is empty or contains only one list
if not lists:
return None
if len(lists) == 1:
return lists[0]

# Split the list into two halves
mid = len(lists) // 2
l, r = self.mergeKLists(lists[:mid]), self.mergeKLists(lists[mid:])

# Merge the two halves
return self.merge(l, r)

def merge(self, l, r):
# Initialize a dummy node to help with merging
dummy = p = ListNode()

# Merge the two lists
while l and r:
if l.val < r.val:
p.next = l
l = l.next
else:
p.next = r
r = r.next
p = p.next

# Attach the remaining elements, if any
p.next = l or r

# Return the merged list, which starts at dummy.next
return dummy.next
Loading

0 comments on commit 8d5a34d

Please sign in to comment.