uz
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

LeetCode, GeeksForGeeks Problem of the day solution

Kanalga Telegram’da o‘tish

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

Ko'proq ko'rsatish
1 250
Obunachilar
+224 soatlar
+147 kunlar
+2930 kunlar
Postlar arxiv
void solve(Node* root, vector&v) { if(root==NULL) { return ; } if(root->left!=NULL and root->right==NULL) { v.push_back(root->left->data); } if(root->left==NULL and root->right!=NULL) { v.push_back(root->right->data); } solve(root->left,v); solve(root->right,v); } vector noSibling(Node* node) { // code here vectorans; solve(node,ans); if(ans.size()==0) { return {-1}; } sort(ans.begin(),ans.end()); return ans; }

GFG | Problem of the day :

class Solution { private: void dfs(vector &v, Node *node) { if(node->left) { dfs(v, node->left); } v.push_back(node->data); if(node->right) { dfs(v, node->right); } } void makeTree(vector &A, Node *dummy, Node *left, int ind) { if(ind == A.size()) { dummy->left = left; return; } Node *node = new Node(A[ind]); node->left = left; ind++; if(ind == A.size()) { dummy->left = node; return; } Node *right = new Node(A[ind]); node->right = right; ind++; if(ind == A.size()) { dummy->left = node; return; } makeTree(A, dummy, node, ind); } public: //Function to serialize a tree and return a list containing nodes of tree. vector serialize(Node *root) { //Your code here vector v; dfs(v, root); return v; } //Function to deserialize a list and construct the tree. Node * deSerialize(vector &A) { //Your code here Node *left = new Node(A[0]); Node *dummy = new Node(NULL); makeTree(A, dummy, left, 1); return dummy->left; } };

GFG | Problem of the day :

class Solution { public: string reversePrefix(string word, char ch) { int l = word.length(); string ans = ""; int j = -1; for (int i = 0; i < l; i++) { if (word[i] == ch) { j = i; break; } } if (j != -1) { string k = word.substr(0, j + 1); reverse(k.begin(), k.end()); word = word.substr(j + 1); return k + word; } return word; } };

LeetCode | Daily challenge :

class Solution { bool isVowel(char ch){ if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u') return true; return false; } public: // task is to complete this function // function should return head to the list after making // necessary arrangements struct Node* arrangeCV(Node *head) { if(head == NULL) return head; vector vows; vector cons; while(head != NULL){ if(isVowel(head->data)) vows.push_back(head->data); else cons.push_back(head->data); head = head->next; } struct Node* ans = new Node(0); struct Node* curr = ans; for(int i=0; inext = new Node(vows[i]); curr = curr->next; } for(int i=0; inext = new Node(cons[i]); curr = curr->next; } return ans->next; } };

GFG | Problem of the day :

class Solution { public: long long wonderfulSubstrings(string word) { vector count(1024, 0); long long result = 0; int prefixXor = 0; count[prefixXor] = 1; for (char ch : word) { int charIndex = ch - 'a'; prefixXor ^= 1 << charIndex; result += count[prefixXor]; for (int i = 0; i < 10; i++) { result += count[prefixXor ^ (1 << i)]; } count[prefixXor]++; } return result; } };

LeetCode | Daily challenge :

class Solution { public: //Function to add two numbers represented by linked list. void removeleadz(struct Node* &a){ struct Node* temp = a; while(temp and temp->data==0){ temp=temp->next; } a = temp; } void reverse(struct Node *&prev,struct Node *&curr){ while(curr){ struct Node *t = curr->next; curr->next = prev; prev=curr; curr=t; } } struct Node* addTwoLists(struct Node* num1, struct Node* num2){ removeleadz(num1); removeleadz(num2); if(!num1 and !num2){ struct Node* p = new Node(0); return p; } if(!num2){ return num1; } if(!num1){ return num2; } struct Node* one = NULL; reverse(one,num1); struct Node* two = NULL; reverse(two,num2); struct Node* x=new Node(-1); struct Node* newhead = x; int prevcarry = 0; while(one and two){ int sum = one ->data + two->data; int sumwithcarry = sum+prevcarry; int newcarry = (sumwithcarry)/10; int toadd = sumwithcarry%10; struct Node* temp=new Node(toadd); x->next = temp; x=x->next; prevcarry = newcarry; one = one -> next; two = two -> next; } while(one){ int sum = one ->data + prevcarry; int newcarry = sum/10; int toadd = sum%10; struct Node* temp=new Node(toadd); x->next = temp; x=x->next; one = one ->next; prevcarry = newcarry; } while(two){ int sum = two ->data + prevcarry; int newcarry = sum/10; int toadd = sum%10; struct Node* temp=new Node(toadd); x->next = temp; x=x->next; two = two ->next; prevcarry = newcarry; } if(prevcarry){ struct Node* m = new Node(prevcarry); x->next = m; x=x->next; } struct Node* z = NULL; reverse(z,newhead->next); return z; } };

GFG | Problem of the day :

class Solution { public: int minOperations(vector& nums, int k) { int ans=0; for(auto x:nums){ ans^=x; } ans^=k; int res=0; while(ans>0){ if(ans&1){ res++; } ans=ans>>1; } return res; } };

LeetCode | Daily challenge :

class Solution { public: Node* deleteK(Node *head,int k){ if(k==1)return nullptr; Node* pre=head; Node* curr=head->next; for(int i=2;curr!=nullptr;i++){ if(i%k==0){ pre->next=curr->next; curr=curr->next; } else{ pre=pre->next; curr=curr->next; } } return head; } };

GFG | Problem of the day :

class Solution { vectorson,sonD,fatherD; public: void sonDFS(vector adj[], int i, int top){ for(auto x : adj[i]){ if(x == top) continue; sonDFS(adj,x, i); son[i] += son[x] + 1; sonD[i] += (sonD[x] + son[x] + 1); } } void fatherDFS(vector adj[], int i, int top, int N){ if(top != -1){ fatherD[i] = fatherD[top] + (sonD[top] - sonD[i] - son[i] - 1) + (N - son[i] - 1); } for(auto x : adj[i]){ if(x == top) continue; fatherDFS(adj, x, i, N); } } vector sumOfDistancesInTree(int n, vector>& edges) { son.resize(n, 0); sonD.resize(n, 0); fatherD.resize(n,0); vector adj[n]; for(auto x : edges){ adj[x[0]].push_back(x[1]); adj[x[1]].push_back(x[0]); } sonDFS(adj, 0, -1); fatherDFS(adj, 0, -1, n); vectorres(n); for(int i=0;i

LeetCode | Daily challenge :

class Solution{ public: Node* deleteMid(Node* head) { Node *temp = head; Node *slow = head; Node *fast = head; if(head->next == NULL || head == NULL) return NULL; while(slow && fast->next && fast->next->next) { temp = slow; slow = slow->next; fast = fast->next->next; } if(fast->next == NULL) { temp->next = slow->next; free(slow); } if(fast->next && fast->next->next == NULL) { temp = slow->next; slow->next = slow->next->next; free(temp); } return head; } };

GFG | Problem of the day :