en
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

LeetCode, GeeksForGeeks Problem of the day solution

Open in Telegram

Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd

Show more
1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution { public: ListNode* doReverse(ListNode* node){ ListNode* temp = NULL; while(node != NULL){ ListNode* nex = node->next; node->next = temp; temp = node; node = nex; } return temp; } void reorderList(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while(fast->next != NULL && fast->next->next != NULL){ fast = fast->next->next; slow = slow->next; } ListNode* secondHalf = slow->next; slow->next = NULL; secondHalf = doReverse(secondHalf); ListNode* A = head; ListNode* B = secondHalf; while(A != NULL && B != NULL){ ListNode* A_next = A->next; ListNode* B_next = B->next; A->next = B; B->next = A_next; A = A_next; B = B_next; } } };

LeetCode | Daily challenge :

class Solution { public: vector Series(int n) { int MOD=1e9+7; vector dp(n+1,0); dp[0]=0; dp[1]=1; for(int i=2;i<=n;i++){ dp[i]=(dp[i-1]+ dp[i-2]) % MOD; } return dp; } };

GFG | Problem of the day :

class Solution { public: bool isPalindrome(ListNode* head) { ListNode* head2 = head; ListNode* slow = head; ListNode* fast = head; while(fast != nullptr && fast->next != nullptr){ slow = slow ->next; fast = fast->next->next; } ListNode* prev= nullptr; while(slow != nullptr){ ListNode* curr = slow->next; slow->next = prev; prev = slow; slow = curr; } while(head2 != nullptr && prev != nullptr){ if(head2->val != prev->val){ return false; } head2 = head2->next; prev = prev->next; } return true; } };

LeetCode | Daily challenge :

class Solution { public: vector diagonalSum(Node* root) { queueq; q.push(root); vectorans; int sum=0; while(!q.empty()) { int size=q.size(); for(int i=0;idata; if(temp->left) q.push(temp->left); if(temp->right) { temp=temp->right; } else break; } q.pop(); } ans.push_back(sum); sum=0; } return ans; } };

GFG | Problem of the day :

class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* curr = NULL; while(head != NULL){ ListNode* temp = head->next; head->next = curr; curr = head; head = temp; } head = curr; return head; } };

LeetCode | Daily challenge :

class Solution{ public: //Function to store the zig zag order traversal of tree in a list. vector zigZagTraversal(Node* root) { // Code here queueq; vectorv; q.push(root); bool flag=false; while(!q.empty()){ int n=q.size(); int siz=v.size(); while(n--){ Node*curr=q.front(); q.pop(); v.push_back(curr->data); if(curr->left){ q.push(curr->left); } if(curr->right){ q.push(curr->right); } } if(flag){ reverse(v.begin()+siz,v.end()); } flag=!flag; } return v; } };

GFG | Problem of the day :

class Solution { public: ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) { ListNode* temp=list1; ListNode* temp2=NULL; int n=1; int step=b-a+1; while(temp!=NULL and n!=a){ temp=temp->next; n++; } temp2=temp->next; temp->next=list2; while(temp2!=NULL and step!=0){ temp2=temp2->next; step--; } while(list2->next!=NULL){ list2=list2->next; } list2->next=temp2; return list1; } };

LeetCode | Daily challenge :

class Solution { public: void solve(Node *root,int len,int& maxLen,int sum,int& maxSum){ if(root==NULL){ if(len>maxLen){ maxLen = len; maxSum = sum; } else if(len==maxLen){ maxSum = max(sum, maxSum); } return; } sum = sum + root->data; solve(root->left, len+1, maxLen, sum, maxSum); solve(root->right, len+1, maxLen, sum, maxSum); } int sumOfLongRootToLeafPath(Node *root) { int len=0; int maxLen=0; int sum=0; int maxSum=INT_MIN; solve(root, len, maxLen, sum, maxSum); return maxSum; } };

GFG | Problem of the day :

class Solution { public: int leastInterval(vector<char>& tasks, int n) { unordered_map<char,int>mp; int time=0; for(auto it : tasks){ mp[it]++; } priority_queue<int>pq; for(auto it : mp){ pq.push(it.second); } while(!pq.empty()){ vector<int>temp; for(int i=0;i<n+1;i++){ if(!pq.empty()){ int freq=pq.top(); pq.pop(); freq--; temp.push_back(freq); } } for(auto it : temp){ if(it > 0){ pq.push(it); } } if(pq.empty()){ time +=temp.size(); } else{ time += n+1; } } return time; } };

LeetCode | Daily challenge :

class Solution{ public: int dsu(vector>& parent, int x){ if(parent[x].first == x){ return x; } return parent[x].first = dsu(parent, parent[x].first); } vector maximumWeight(int n, vector> edges, int q, vector &queries) { // code here vector>Q(q); for(int i = 0; i < q; i++){ Q[i].first = queries[i]; Q[i].second = i; } sort(Q.begin(), Q.end()); for(int i = 0; i < n-1; i++){ swap(edges[i][0], edges[i][2]); } sort(edges.begin(),edges.end()); vector>parent(n); for(int i = 0; i < n; i++){ parent[i].first = i; parent[i].second = 1; } vectorans(q); int i = 0, last = 0; for(int j = 0; j < q; j++){ while(i < n-1 && edges[i][0] <= Q[j].first){ int u = edges[i][1] - 1, v = edges[i][2] - 1; int paru = dsu(parent, u), parv = dsu(parent, v); int cu = parent[paru].second, cv = parent[parv].second; last -= (cu * (cu - 1)) / 2; last -= (cv * (cv - 1)) / 2; parent[parv].first = paru; parent[paru].second += parent[parv].second; dsu(parent, parv); cu += cv; last += (cu * (cu - 1)) / 2; i++; } ans[Q[j].second] = last; } return ans; } };

GFG | Problem of the day :