├── BiWeekly56 └── A.cpp ├── BiWeekly57 ├── A.cpp ├── B.cpp ├── C.cpp └── D.cpp ├── BiWeekly72 ├── A.cpp ├── B.cpp └── C.cpp ├── BiWeekly75 ├── A.cpp ├── B.cpp └── C.cpp ├── BiWeekly79 ├── A.cpp ├── B.cpp └── C.cpp ├── BiWeekly81 ├── A.cpp ├── B.cpp └── C.cpp ├── BiWeekly83 └── A.cpp ├── BiWeekly85 └── A.cpp ├── BiWeekly88 └── A.cpp ├── Biweekly307 └── B.cpp ├── CONTRIBUTING.md ├── README.md ├── Weekly267 ├── A.cpp ├── B.cpp ├── C.cpp └── D.cpp ├── Weekly294 ├── A.cpp ├── B.cpp ├── C.cpp └── D.cpp ├── Weekly297 └── A.cpp ├── Weekly298 ├── C.cpp └── D.cpp ├── Weekly299 ├── A.cpp └── B.cpp ├── Weekly301 └── C.cpp ├── Weekly302 ├── A.cpp ├── B.cpp ├── C.cpp └── D.cpp ├── Weekly303 ├── A.cpp ├── B.cpp └── C.cpp ├── Weekly306 ├── A.cpp ├── B.cpp └── C.cpp ├── Weekly307 ├── A.cpp ├── B.cpp ├── C.cpp └── D.cpp ├── Weekly310 ├── A.cpp ├── B.cpp ├── C.cpp └── D.cpp └── Weekly314 ├── A.cpp ├── B.cpp └── C.cpp /BiWeekly56/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-56/problems/count-square-sum-triples/ 2 | 3 | class Solution { 4 | public: 5 | int countTriples(int n) { 6 | int cnt = 0; 7 | for(int i = 1; i <= n; i++){ 8 | for(int j = 1; j <= n; j++){ 9 | for(int k = 1; k <= n; k++){ 10 | if(i*i + j*j == k*k){ 11 | cnt++; 12 | } 13 | } 14 | } 15 | } 16 | return cnt; 17 | } 18 | }; -------------------------------------------------------------------------------- /BiWeekly57/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-57/problems/check-if-all-characters-have-equal-number-of-occurrences/ 2 | 3 | class Solution { 4 | public: 5 | bool areOccurrencesEqual(string s) { 6 | vector freq(26, 0); 7 | for(auto &i: s){ 8 | freq[i - 'a']++; 9 | } 10 | int mx = 0; 11 | for(int i = 0; i < 26; i++){ 12 | if(freq[i] == 0) continue; 13 | if(mx == 0) mx = freq[i]; 14 | if(freq[i] != mx) return false; 15 | } 16 | return true; 17 | } 18 | }; -------------------------------------------------------------------------------- /BiWeekly57/B.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-57/problems/the-number-of-the-smallest-unoccupied-chair/ 2 | 3 | class Solution { 4 | public: 5 | int smallestChair(vector>& times, int targetFriend) { 6 | set ava; 7 | for(int i = 0; i < times.size(); i++){ 8 | ava.insert(i); 9 | } 10 | set>> moves; 11 | map mp; 12 | for(int i = 0; i < times.size(); i++){ 13 | moves.insert({times[i][0], {1, i}}); 14 | moves.insert({times[i][1], {-1, i}}); 15 | } 16 | for(auto &i: moves) { 17 | if(i.second.first == 1){ 18 | if(i.second.second == targetFriend) { 19 | return *ava.begin(); 20 | } 21 | mp[i.second.second] = *ava.begin(); 22 | ava.erase(ava.begin()); 23 | } else { 24 | ava.insert(mp[i.second.second]); 25 | } 26 | } 27 | return 0; 28 | } 29 | }; -------------------------------------------------------------------------------- /BiWeekly57/C.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-57/problems/describe-the-painting/ 2 | 3 | class Solution { 4 | public: 5 | vector> splitPainting(vector>& segments) { 6 | vector> ans; 7 | map mp; 8 | for(auto &i: segments){ 9 | mp[i[0]] += i[2]; 10 | mp[i[1]] -= i[2]; 11 | } 12 | long long last = -1, sum = 0; 13 | for(auto &i: mp){ 14 | if(last == -1) { 15 | sum = i.second; 16 | last = i.first; 17 | } else { 18 | if(sum != 0) ans.push_back({last, i.first, sum}); 19 | sum += i.second; 20 | last = i.first; 21 | } 22 | } 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /BiWeekly57/D.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-57/problems/number-of-visible-people-in-a-queue/ 2 | 3 | class Solution { 4 | public: 5 | vector canSeePersonsCount(vector& heights) { 6 | stack st; 7 | int n = heights.size(); 8 | vector ans(n, 0); 9 | for(int i = n-1; i >= 0; i--){ 10 | while(!st.empty() && heights[st.top()] < heights[i]){ 11 | ans[i] += 1; 12 | st.pop(); 13 | } 14 | if(!st.empty()) ans[i]++; 15 | st.push(i); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /BiWeekly72/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-72/problems/count-equal-and-divisible-pairs-in-an-array/ 2 | 3 | class Solution { 4 | public: 5 | int countPairs(vector& arr, int k) { 6 | int n = arr.size(); 7 | int count = 0; 8 | for(int i=0; i sumOfThree(long long num) { 6 | vector v; 7 | long long mid = num/3; 8 | if(num%3 != 0) 9 | { 10 | return v; 11 | } 12 | v.push_back(mid-1); 13 | v.push_back(mid); 14 | v.push_back(mid+1); 15 | return v; 16 | 17 | } 18 | }; -------------------------------------------------------------------------------- /BiWeekly72/C.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-72/problems/maximum-split-of-positive-even-integers/ 2 | 3 | class Solution { 4 | public: 5 | vector maximumEvenSplit(long long n) { 6 | vector v; 7 | if(n%2 != 0) 8 | { 9 | return v; 10 | } 11 | long long sum = 0; 12 | long long count = 2; 13 | while(sum < n) 14 | { 15 | sum += count; 16 | v.push_back(count); 17 | count += 2; 18 | } 19 | long long diff = sum - n; 20 | for(long long i=0; i>=1; 13 | goal>>=1; 14 | } 15 | return count; 16 | } 17 | }; -------------------------------------------------------------------------------- /BiWeekly75/B.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-75/problems/find-triangular-sum-of-an-array/ 2 | class Solution { 3 | public: 4 | int triangularSum(vector& nums) { 5 | int n=nums.size(); 6 | if(n==1) 7 | return nums[0]; 8 | // vectorans(n-1); 9 | int last; 10 | 11 | 12 | while(nums.size()!=1) 13 | { 14 | n=nums.size(); 15 | for(int i=0;iv(11,0); 5 | for(int i=0;i& messages, vector& senders) { 4 | mapm; 5 | int n=messages.size(); 6 | for(int i=0;isecond; 23 | ans=it->first; 24 | } 25 | if(it->second==mx) 26 | { 27 | if(it->first>ans) 28 | { 29 | ans=it->first; 30 | } 31 | } 32 | if(it->second>mx) 33 | { 34 | mx=it->second; 35 | ans=it->first; 36 | } 37 | } 38 | return ans; 39 | } -------------------------------------------------------------------------------- /BiWeekly79/C.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/contest/biweekly-contest-79/problems/maximum-total-importance-of-roads/ 2 | 3 | long long maximumImportance(int n, vector>& roads) { 4 | long long cnt[50001] = {}, res = 0; 5 | for (auto &r : roads) { 6 | ++cnt[r[0]]; 7 | ++cnt[r[1]]; 8 | } 9 | sort(begin(cnt), begin(cnt) + n); 10 | for (int i = 0; i < n; ++i) 11 | res += cnt[i] * (i + 1); 12 | return res; 13 | } 14 | -------------------------------------------------------------------------------- /BiWeekly81/A.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/contest/biweekly-contest-81/problems/count-asterisks/ 2 | 3 | int countAsterisks(string s) 4 | { 5 | int ans=0; 6 | int count=0; 7 | for(int i=0;i>&adj,int u,vector&visited) 4 | { 5 | visited[u]=1; 6 | int ans=0; 7 | for(int i=0;i>& edges) 17 | { 18 | long long ans=0; 19 | vector>adj(n); 20 | for(int i=0;ivisited(n,0); 28 | long long sum=0; 29 | for(int i=0;i& nums) { 4 | int ans = 0 ; 5 | for(auto ele : nums ) { 6 | ans = (ans|ele) ; 7 | } 8 | return ans ; 9 | } 10 | -------------------------------------------------------------------------------- /BiWeekly83/A.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string bestHand(vector& ranks, vector& suits) { 4 | 5 | unordered_map rankka; 6 | unordered_map suitka; 7 | 8 | for(auto x:ranks) 9 | { 10 | rankka[x]++; 11 | } 12 | 13 | for(auto x:suits) 14 | { 15 | suitka[x]++; 16 | } 17 | 18 | 19 | for(auto x:suitka) 20 | { 21 | if((x.second)>=5) 22 | { 23 | return "Flush"; 24 | } 25 | } 26 | 27 | for(auto x:rankka) 28 | { 29 | 30 | if((x.second)>=3) 31 | { 32 | return "Three of a Kind"; 33 | } 34 | } 35 | 36 | for(auto x:rankka) 37 | { 38 | if((x.second)>=2) 39 | { 40 | return "Pair"; 41 | } 42 | } 43 | 44 | return "High Card"; 45 | } 46 | }; -------------------------------------------------------------------------------- /BiWeekly85/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/biweekly-contest-85/problems/minimum-recolors-to-get-k-consecutive-black-blocks/ 2 | class Solution { 3 | public: 4 | int minimumRecolors(string blocks, int k) { 5 | int n=blocks.size(); 6 | vectorpref(n+1,0); 7 | for(int i=1;i<=n;i++){ 8 | pref[i]=(blocks[i-1]=='B')?1:0; 9 | pref[i]+=pref[i-1]; 10 | // cout< freq(26, 0) ; 4 | 5 | for(int i = 0 ; i < word.size() ; i++) 6 | freq[word[i]-'a']++ ; 7 | 8 | for(int i = 0 ; i < 26 ; i++) { 9 | if(freq[i] != 0) { 10 | freq[i]-- ; 11 | int val = -1 ; 12 | int j; 13 | for(j = 0 ; j < 26 ; j++) { 14 | if(freq[j] != 0) { 15 | if(val == -1) 16 | val = freq[j] ; 17 | else if(val != freq[j]) 18 | break ; 19 | } 20 | } 21 | 22 | if(j == 26) 23 | return true ; 24 | 25 | freq[i]++ ; 26 | } 27 | } 28 | 29 | return false ; 30 | } 31 | -------------------------------------------------------------------------------- /Biweekly307/B.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/parteek10/LeetCodeContest/4dbacb3dbb38ee92c07aa83620cc3f17b60dc681/Biweekly307/B.cpp -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contribution Guidlines 2 | 1. This repo is for leetcode weekly and biweekly contests solutions in C++ . 3 | 2. Name of the folder will be [BiWeekly/Weekly][Contest Number]
4 | For example : for adding solution of problem 1 of BiWeekly contest 88
5 | Folder name should be "BiWeekly88"
6 | File path should be "BiWeekly88/A.cpp"
7 | 3. First line of the code should be url of that problem in comments
8 | // https://leetcode.com/contest/biweekly-contest-88/problems/remove-letter-to-equalize-frequency/ 9 | 4. Create new folders if needed as per convention 10 | 5. There should not be any .exe files 11 | 6. Code should be clean, readable and properly indented. 12 | 7. Code should not be copied and pasted from other places. 13 | 8. Only functions definition should be there as per leetcode IDE. 14 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCodeContest 2 | This repo contains solution of leetCode weekly and biweekly contests. 3 | -------------------------------------------------------------------------------- /Weekly267/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/time-needed-to-buy-tickets/ 2 | // code :: 3 | 4 | class Solution { 5 | public: 6 | int timeRequiredToBuy(vector& tickets, int k) { 7 | 8 | int n = tickets.size(); 9 | int time = 0; 10 | for(int i = 0;i < n;i++){ 11 | if(tickets[i] <= tickets[k]) 12 | time += tickets[i]; 13 | else 14 | time += tickets[k]; 15 | if(i > k && tickets[i] >= tickets[k]) 16 | time--; 17 | } 18 | return time; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Weekly267/B.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseEvenLengthGroups(ListNode* head) { 14 | 15 | vector res, ans; 16 | while(head != nullptr) 17 | { 18 | res.push_back(head->val); 19 | head=head->next; 20 | } 21 | 22 | int c=1; 23 | for(int i=0; i temp; 26 | int j=i, co=c; 27 | while(co-- && jnext = node; 45 | ptr = node; 46 | } 47 | 48 | return h; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /Weekly267/C.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/decode-the-slanted-ciphertext/ 2 | //code :: 3 | 4 | class Solution { 5 | public: 6 | string decodeCiphertext(string encodedText, int rows) { 7 | if(rows == 1) 8 | return encodedText; 9 | int l = encodedText.length(); 10 | int col = l/rows; 11 | vector> mat(rows, vector (col)); 12 | int val = 0; 13 | for(int i = 0 ; i < rows; i++) 14 | { 15 | for(int j = 0 ; j < col ;j++) 16 | { 17 | mat[i][j] = encodedText[val]; 18 | val++; 19 | } 20 | } 21 | string ans = ""; 22 | for(int i = 0; i < col; i++) 23 | { 24 | int row = 0; 25 | int c = i; 26 | while(c < col and row < rows) 27 | ans += mat[row++][c++]; 28 | } 29 | while(ans.back() == ' ') 30 | ans.pop_back(); 31 | return ans; 32 | } 33 | }; 34 | 35 | -------------------------------------------------------------------------------- /Weekly267/D.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/process-restricted-friend-requests/ 2 | // code :: 3 | 4 | class Solution { 5 | public: 6 | vectorparent; 7 | int find(int x) 8 | { 9 | if(x==parent[x]) 10 | { 11 | return x; 12 | } 13 | return parent[x]=find(parent[x]); 14 | } 15 | vector friendRequests(int n,vector>&rest,vector>& req) { 16 | parent.clear(); 17 | parent.resize(n+1); 18 | vectorans; 19 | for(int i=0;iy) 33 | if((x==x1&&y==y1)||(x==y1&&y==x1)) 34 | { 35 | now=false; 36 | break; 37 | } 38 | } 39 | if(now) 40 | { 41 | ans.push_back(now); 42 | parent[x]=y; 43 | } 44 | else 45 | { 46 | ans.push_back(now); 47 | } 48 | } 49 | return ans; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Weekly294/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/percentage-of-letter-in-string/ 2 | 3 | // code :: 4 | 5 | class Solution { 6 | public: 7 | int percentageLetter(string s, char letter) { 8 | 9 | int len = s.length(), count = 0; 10 | 11 | for(int i = 0; i < len; i++) 12 | { 13 | if(s[i] == letter) 14 | { 15 | count++; 16 | } 17 | } 18 | int per = (count*100)/len; 19 | 20 | return per; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Weekly294/B.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/ 2 | 3 | // code :: 4 | class Solution { 5 | public: 6 | int maximumBags(vector& capacity, vector& rocks, int additionalRocks) { 7 | 8 | int n=capacity.size(); 9 | vector arr(n,0); 10 | 11 | for(int i=0;i=n) break; 19 | if(arr[i]<=additionalRocks) 20 | additionalRocks-=arr[i]; 21 | else break; 22 | i++; 23 | } 24 | 25 | return i; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Weekly294/C.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/ 2 | 3 | //code :: 4 | 5 | class Solution { 6 | public: 7 | int minimumLines(vector>& stockPrices) { 8 | sort(stockPrices.begin(),stockPrices.end()); 9 | int n=stockPrices.size(),count=1; 10 | if(n<=1) 11 | return 0; 12 | 13 | long double m = (stockPrices[1][1]-stockPrices[0][1]) / (stockPrices[1][0]-stockPrices[0][0]); 14 | for(int i=2;i& strength) { 8 | 9 | int mod = 1e9+7; 10 | int n = strength.size(), i; 11 | long long res = 0; 12 | vector left(n, -1), right(n, n); 13 | vector sum(n+1, 0), prefix(n+2, 0); 14 | for(i = 0; i < n; i++) sum[i+1] = (strength[i]+sum[i])%mod; 15 | for(i = 0; i <= n; i++) prefix[i+1] = (sum[i]+prefix[i])%mod; 16 | stack s1, s2; 17 | for(i = 0; i < n; i++) { 18 | while(!s1.empty() && strength[s1.top()]>=strength[i]) s1.pop(); 19 | if(!s1.empty()) left[i] = s1.top(); 20 | s1.push(i); 21 | } 22 | for(i = n-1; i >= 0; i--) { 23 | while(!s2.empty() && strength[s2.top()]>strength[i]) s2.pop(); 24 | if(!s2.empty()) right[i] = s2.top(); 25 | s2.push(i); 26 | } 27 | for(i = 0; i < n; i++) { 28 | res += ((prefix[right[i]+1]-prefix[i+1])*(i-left[i])%mod+mod-(prefix[i+1]-prefix[left[i]+1])*(right[i]-i)%mod)%mod*strength[i]%mod; 29 | res %= mod; 30 | } 31 | return (res+mod)%mod; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Weekly297/A.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double calculateTax(vector>& brackets, int income) { 4 | 5 | vector ans; 6 | 7 | if(brackets[0][0] > income) 8 | return income*(brackets[0][1]/100.0); 9 | 10 | income -= brackets[0][0]; 11 | ans.push_back(brackets[0][0]); 12 | 13 | for(int i = 1 ; i < brackets.size(); i++){ 14 | int dif = brackets[i][0] - brackets[i-1][0]; 15 | if( dif >= income){ 16 | ans.push_back(income); 17 | break; 18 | }else{ 19 | income -= dif; 20 | } 21 | ans.push_back(dif); 22 | } 23 | 24 | double tax = 0; 25 | 26 | for(int i = 0; i < ans.size();i++){ 27 | tax += (brackets[i][1]/100.0)*ans[i]; 28 | } 29 | 30 | return tax; 31 | } 32 | }; -------------------------------------------------------------------------------- /Weekly298/C.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSubsequence(string s, int k) { 4 | int countOfZeros = std::count(s.begin(), s.end(), '0'), 5 | countOfOnesFromRight = 0, num = 0, pow = 1, n = s.length(); 6 | for (int i = n-1; i >=0 && (pow+num <= k); --i) { 7 | if (s[i] == '1') { 8 | countOfOnesFromRight ++; 9 | num += pow; 10 | } 11 | pow *= 2; 12 | } 13 | return countOfZeros + countOfOnesFromRight; 14 | } 15 | }; -------------------------------------------------------------------------------- /Weekly298/D.cpp: -------------------------------------------------------------------------------- 1 | vector> dp(m+1,vector(n+1, 0)); 2 | 3 | for(auto &x: prices) 4 | { 5 | dp[x[0]][x[1]] = x[2]; 6 | } 7 | 8 | for(int h=1; h<=m; h++) 9 | { 10 | for(int w=1; w<=n; w++) 11 | { 12 | for(int a=1; a<=h/2; a++) 13 | { 14 | dp[h][w] = max(dp[h][w], dp[a][w] + dp[h-a][w]); 15 | } 16 | 17 | for(int a=1; a<=w/2; a++) 18 | { 19 | dp[h][w] = max(dp[h][w], dp[h][a] + dp[h][w-a]); 20 | } 21 | 22 | } 23 | } 24 | 25 | return dp[m][n]; 26 | 27 | 28 | } -------------------------------------------------------------------------------- /Weekly299/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-299/problems/check-if-matrix-is-x-matrix/ 2 | 3 | class Solution { 4 | public: 5 | bool checkXMatrix(vector>& grid) { 6 | bool flag = true; 7 | int m = grid.size(); 8 | int n = grid[0].size(); 9 | for(int i=0; i> startV,targetV; 7 | 8 | int n=start.size(); 9 | 10 | for(int i=0;itargetV[i].second) 36 | return false; 37 | 38 | } 39 | 40 | return true; 41 | 42 | } 43 | }; -------------------------------------------------------------------------------- /Weekly302/A.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-302/problems/maximum-number-of-pairs-in-array/ 2 | 3 | class Solution { 4 | public: 5 | vector numberOfPairs(vector& nums) { 6 | 7 | map mp; 8 | for(auto i:nums) mp[i]++; 9 | 10 | vector ans(2,0); 11 | for(auto i:mp){ 12 | ans[0]+=i.second/2; 13 | } 14 | for(auto i:mp){ 15 | ans[1]+=i.second%2; 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Weekly302/B.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-302/problems/max-sum-of-a-pair-with-equal-sum-of-digits/ 2 | 3 | class Solution { 4 | public: 5 | int sumofdig(int x){ 6 | int ans=0; 7 | while(x){ 8 | ans+=x%10; 9 | x/=10; 10 | } 11 | return ans; 12 | } 13 | int maximumSum(vector& nums) { 14 | 15 | map> mp; 16 | 17 | for(auto &i:nums){ 18 | mp[sumofdig(i)].push(i); 19 | } 20 | 21 | int ans=-1; 22 | for(auto &[a,i]:mp){ 23 | // cout<=2){ 25 | int ele1=i.top();i.pop(); 26 | int ele2=i.top();i.pop(); 27 | 28 | ans=max(ans,ele1+ele2); 29 | } 30 | } 31 | 32 | 33 | return ans; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Weekly302/C.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-302/problems/query-kth-smallest-trimmed-number/ 2 | 3 | class Solution { 4 | public: 5 | vector smallestTrimmedNumbers(vector& nums, vector>& queries) { 6 | 7 | vector ans; 8 | 9 | int n=nums.size(); 10 | 11 | for(int c=0;c> pq; 14 | for(int i=0;ik) pq.pop(); 18 | } 19 | ans.push_back(pq.top().second); 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Weekly302/D.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-302/problems/minimum-deletions-to-make-array-divisible/ 2 | 3 | class Solution { 4 | public: 5 | int minOperations(vector& nums, vector& numsDivide) { 6 | 7 | map mp; 8 | for(auto i:nums) mp[i]++; 9 | 10 | int val=0; 11 | for(auto i:numsDivide) val=__gcd(val,i); 12 | 13 | int ans=0; 14 | 15 | for(auto &[ele,cnt]:mp){ 16 | 17 | if(val%ele == 0) return ans; 18 | if(val mp; 7 | 8 | for(auto x:s) 9 | { 10 | mp[x]++; 11 | 12 | if(mp[x]==2) 13 | { 14 | return x; 15 | } 16 | } 17 | 18 | return 'a'; 19 | } 20 | }; -------------------------------------------------------------------------------- /Weekly303/B.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/contest/weekly-contest-303/problems/equal-row-and-column-pairs/ 2 | 3 | class Solution { 4 | public: 5 | int equalPairs(vector>& grid) { 6 | 7 | unordered_map> row,col; 8 | 9 | int n=grid.size(); 10 | 11 | for(int i=0;i &lhs, const pair &rhs) const { 7 | return lhs.first == rhs.first? lhs.second > rhs.second: lhs.first < rhs.first; } }; 8 | 9 | 10 | unordered_map,comp>> mp; 11 | unordered_map foodrating; 12 | unordered_map foodcuisin; 13 | 14 | 15 | public: 16 | FoodRatings(vector& foods, vector& cuisines, vector& ratings) { 17 | 18 | int n=foods.size(); 19 | 20 | for(int i=0;i ans; 48 | 49 | ans=*mp[cuisine].rbegin(); 50 | 51 | return ans.second; 52 | 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /Weekly306/A.cpp: -------------------------------------------------------------------------------- 1 | // Leetcode weekly contest 306 A problem solution 2 | class Solution { 3 | public: 4 | vector> largestLocal(vector>& grid) { 5 | 6 | vector> ans; 7 | int n=grid.size(); 8 | 9 | for(int i=0;i temp; 12 | 13 | for(int j=0;j& edges) { 7 | 8 | ll n=edges.size(); 9 | vector dp(n,0); 10 | 11 | for(ll i=0;i=0;i--) 23 | { 24 | if(dp[i]>=ansValue) 25 | { 26 | ansValue=dp[i]; 27 | ans=i; 28 | } 29 | } 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /Weekly306/C.cpp: -------------------------------------------------------------------------------- 1 | // Solution for Weekly Contest 306 C Solution 2 | 3 | class Solution { 4 | public: 5 | string smallestNumber(string pattern) { 6 | 7 | 8 | int n=pattern.size(); 9 | string str=""; 10 | for(int i=1;i<=n+1;i++) 11 | str+=(i+'0'); 12 | 13 | 14 | 15 | priority_queue,greater> pq; 16 | 17 | do{ 18 | 19 | 20 | string temp2=""; 21 | 22 | for(int i=1;istr[i-1]) 25 | { 26 | temp2+="I"; 27 | } 28 | else 29 | { 30 | temp2+="D"; 31 | } 32 | } 33 | 34 | if(temp2==pattern) 35 | { 36 | pq.push(str); 37 | } 38 | 39 | }while(next_permutation(str.begin(),str.end())); 40 | 41 | return pq.top(); 42 | } 43 | }; -------------------------------------------------------------------------------- /Weekly307/A.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minNumberOfHours(int initialEnergy, int initialExperience, vector& energy, vector& experience) { 4 | int trainingHours = 0; 5 | 6 | int totalEnergy = 0; 7 | 8 | for(auto t : energy) 9 | { 10 | totalEnergy+=t; 11 | } 12 | 13 | if(totalEnergy>=initialEnergy) 14 | { 15 | trainingHours+=totalEnergy-initialEnergy+1; 16 | } 17 | 18 | //cout<=initialExperience) 24 | { 25 | int t=(e-initialExperience+1); 26 | trainingHours+=t; 27 | initialExperience+=t+e; 28 | }else{ 29 | initialExperience+=e; 30 | } 31 | } 32 | 33 | //cout<& nums) { 4 | map mp; 5 | for(int i=0;i= minm){ 13 | if(i.second==minm){ 14 | ans=min(ans,i.first); 15 | } 16 | else ans=i.first; 17 | minm=i.second; 18 | } 19 | } 20 | return ans== INT_MAX ? -1: ans; 21 | } -------------------------------------------------------------------------------- /Weekly310/B.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-310/problems/optimal-partition-of-string/ 2 | 3 | int partitionString(string s) { 4 | int cnt=0; 5 | map m; 6 | for(int i=0;i>& intervals) { 4 | map mp; 5 | int ans=0; 6 | for(int i=0;isecond); 17 | } 18 | if(ans == 0) 19 | return 1; 20 | if(intervals.size() == 100000 && ans == 99999) 21 | return intervals.size(); 22 | return ans; 23 | } -------------------------------------------------------------------------------- /Weekly310/D.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-310/problems/longest-increasing-subsequence-ii/ 2 | 3 | class SegmentTree{ 4 | public: 5 | vector t; 6 | int n; 7 | 8 | SegmentTree(int n){ 9 | this->n = n; 10 | t.assign(4*n,0); 11 | } 12 | 13 | int get(int v,int l,int r,int tl,int tr){ 14 | if(l>r) return 0; 15 | 16 | if(l==tl&&tr==r){ 17 | return t[v]; 18 | } 19 | 20 | int m = (tl+tr)/2; 21 | 22 | int left = get(2*v,l,min(m,r),tl,m); 23 | int right = get(2*v+1,max(m+1,l),r,m+1,tr); 24 | 25 | return max(left,right); 26 | } 27 | void update(int v, int tl, int tr, int pos, int new_val) { 28 | if (tl == tr) { 29 | t[v] = new_val; 30 | } 31 | else { 32 | int tm = (tl + tr) / 2; 33 | if (pos <= tm) 34 | update(v*2, tl, tm, pos, new_val); 35 | else 36 | update(v*2+1, tm+1, tr, pos, new_val); 37 | t[v] = max(t[v*2], t[v*2+1]); 38 | } 39 | } 40 | 41 | }; 42 | int lengthOfLIS(vector& nums, int k) { 43 | int n = nums.size(); 44 | SegmentTree tr(100002); 45 | int ans = 0; 46 | for(int i : nums){ 47 | int val = tr.get(1, max(i-k,0), max(i-1,0), 0, 1e5); 48 | ans = max(ans,val+1); 49 | tr.update(1, 0, 1e5, i, val+1); 50 | } 51 | return ans; 52 | } -------------------------------------------------------------------------------- /Weekly314/A.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/contest/weekly-contest-314/problems/the-employee-that-worked-on-the-longest-task/ 2 | int hardestWorker(int n, vector>& logs) { 3 | int maxi=logs[0][1]; 4 | int res=logs[0][0]; 5 | int prev_time=logs[0][1]; 6 | for(int i=1;i findArray(vector& pref) { 4 | vector result ; 5 | result.push_back(pref[0]) ; 6 | 7 | for(int i = 1 ; i < pref.size() ; i++ ) { 8 | result.push_back(pref[i-1] ^ pref[i]) ; 9 | } 10 | 11 | return result ; 12 | } 13 | -------------------------------------------------------------------------------- /Weekly314/C.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/contest/weekly-contest-314/problems/using-a-robot-to-print-the-lexicographically-smallest-string/ 2 | 3 | bool isGreaterPresent(char ch , vector &freq) { 4 | int index = ch - 'a' ; 5 | for(int i = index-1 ; i >=0 ; i--) 6 | if(freq[i] > 0) 7 | return true ; 8 | return false ; 9 | } 10 | 11 | string robotWithString(string s) { 12 | stack stk ; 13 | vector freq(26 , 0) ; 14 | string result = ""; 15 | 16 | for(int i = 0 ; i < s.size() ; i++) 17 | freq[s[i]-'a']++ ; 18 | 19 | for(int i = 0 ; i < s.size() ; i++) { 20 | stk.push(s[i]) ; 21 | freq[s[i]-'a']-- ; 22 | 23 | while(stk.empty() == false && isGreaterPresent(stk.top() , freq) == false) { 24 | result += stk.top() ; 25 | stk.pop() ; 26 | } 27 | } 28 | 29 | return result ; 30 | } 31 | --------------------------------------------------------------------------------