Skip to content

Commit 36296b6

Browse files
authored
Merge branch 'main' into main
2 parents e024346 + 5add706 commit 36296b6

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

.github/pull_request_template.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### Intuition
2+
<!-- Explain your thought process and initial approach to the problem -->
3+
4+
5+
6+
7+
### Approach
8+
<!-- Describe your solution approach in detail -->
9+
10+
11+
12+
## Code Solution (C++)
13+
14+
```cpp
15+
// Your code goes here
16+
17+
18+
```
19+
20+
21+
22+
23+
## Related Issues
24+
<!-- Link any related issues: Closes #issue_number -->
25+
26+
27+
28+
**By submitting this PR, I confirm that:**
29+
- [ ] This is my original work not totally AI genearted
30+
- [ ] I have tested the solution thoroughly on leetcode
31+
- [ ] I have maintained proper PR description format
32+
- [ ] This is a meaningful contribution, not spam

152. Maximum-Product-Subarray.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 152. Maximum Product Subarray
2+
// By Traversing in Both Directions
3+
// Time Complexity - O(n) and Space Complexity - O(1)
4+
class Solution {
5+
public:
6+
int maxProduct(vector<int>& nums) {
7+
int n = nums.size();
8+
int maxi = INT_MIN;
9+
int ltr = 1, rtl = 1;
10+
for(int i=0; i<n; i++){
11+
if(ltr == 0){
12+
ltr = 1;
13+
}
14+
if(rtl == 0){
15+
rtl = 1;
16+
}
17+
18+
ltr *= nums[i];
19+
int j = n-i-1;
20+
rtl *= nums[j];
21+
22+
maxi = max({ltr,rtl,maxi});
23+
}
24+
return maxi;
25+
}
26+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int differenceOfSums(int n, int m) {
4+
int ans=0;
5+
for(int i=1;i<=n;i++){
6+
if(i%m==0) ans-=i;
7+
else ans+=i;
8+
9+
}
10+
return ans;
11+
}
12+
};

355. Design Twitter.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Twitter {
2+
private:
3+
int time;
4+
unordered_map<int, unordered_set<int>> mp; // Stores followers
5+
unordered_map<int, vector<pair<int, int>>> mp2; // Stores tweets (time, tweetId)
6+
public:
7+
Twitter() {
8+
time=0;
9+
}
10+
11+
void postTweet(int userId, int tweetId) {
12+
mp2[userId].emplace_back(time,tweetId);
13+
time++;
14+
}
15+
16+
vector<int> getNewsFeed(int userId) {
17+
priority_queue<pair<int,int>> pq;
18+
for(auto i: mp2[userId]) pq.push(i);
19+
for(auto i: mp[userId]){
20+
for(auto j: mp2[i]) pq.push(j);
21+
}
22+
vector<int> feed;
23+
int count=10;
24+
while(!pq.empty() && count){
25+
feed.push_back(pq.top().second);
26+
pq.pop();
27+
count--;
28+
}
29+
return feed;
30+
}
31+
32+
void follow(int followerId, int followeeId) {
33+
mp[followerId].insert(followeeId);
34+
}
35+
36+
void unfollow(int followerId, int followeeId) {
37+
mp[followerId].erase(followeeId);
38+
}
39+
};
40+
41+
/**
42+
* Your Twitter object will be instantiated and called as such:
43+
* Twitter* obj = new Twitter();
44+
* obj->postTweet(userId,tweetId);
45+
* vector<int> param_2 = obj->getNewsFeed(userId);
46+
* obj->follow(followerId,followeeId);
47+
* obj->unfollow(followerId,followeeId);
48+
*/

0 commit comments

Comments
 (0)