Skip to content

Commit cba1e3c

Browse files
authored
Merge branch 'main' into file
2 parents 5394a15 + 5bb18e8 commit cba1e3c

5 files changed

+126
-0
lines changed

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+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int differenceOfSums(int n, int m) {
4+
int num1 = 0, num2 = 0;
5+
for (int i = 1; i <= n; i++) {
6+
if (i % m == 0)
7+
num2 += i; // divisible by m
8+
else
9+
num1 += i; // not divisible by m
10+
}
11+
return num1 - num2;
12+
}
13+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
bool isZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
7+
int n = nums.size();
8+
vector<int> diff(n + 1, 0);
9+
10+
for(const auto& q : queries){
11+
int l = q[0], r = q[1];
12+
diff[l] += 1;
13+
if(r + 1 < n){
14+
diff[r + 1] -= 1;
15+
}
16+
}
17+
18+
int coverage = 0;
19+
for(int i = 0; i < n; ++i){
20+
coverage += diff[i];
21+
if(nums[i] > coverage){
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
};

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)