es
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

LeetCode, GeeksForGeeks Problem of the day solution

Ir al canal en Telegram

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

Mostrar más
1 250
Suscriptores
+224 horas
+147 días
+2930 días
Archivo de publicaciones
class Solution { public: int numSquares(int n) { vector squares; int p = 1, curr = 1; while (curr <= n) { curr = pow(p++, 2); squares.push_back(curr); } vector dp(n+1, n+1); dp[0] = 0; for (int i = 1; i <= n; i++) for (auto s : squares) if (s <= i) dp[i] = min(dp[i], dp[i-s]+1); return dp[n]; } };

LeetCode | Daily challenge :

class Solution{ public: /*You are required to complete this method*/ bool check(Node *root) { queue q; int mila=0; q.push(root); while(!q.empty()){ int n=q.size(); for(int i=0;ileft==NULL && t->right==NULL){ mila=1; continue; } if(t->left!=NULL) q.push(t->left); if(t->right!=NULL) q.push(t->right); } if(mila==1 && q.size()>0) return(false); } return(true); } };

GFG | Problem of the day :

class Solution { public: //comaprision function to pass to sort() static bool cmp(pair&a, pair&b) { return a.second > b.second; } string frequencySort(string s) { if(s.length()==0 or s.length()==1 or s.length()==2) return s; string ans=""; unordered_mapm; vector>vm; for(char c: s) { // if(c != ' ') m[c]++; } for(auto it : m) vm.push_back(make_pair(it.first,it.second)); sort(vm.begin(),vm.end(), cmp); for(auto& i : vm) { while(i.second > 0) { ans+= i.first; --i.second; } } return ans; } };

LeetCode | Daily challenge :

class Solution{ public: /* Should return minimum distance between a and b in a tree with given root*/ void traverse(Node *root, unordered_map<int, vector<int>> &adj, unordered_map<int, int> &distance){ if(root == nullptr) return; distance[root->data] = INT_MAX; if(root->left){ adj[root->data].push_back(root->left->data); adj[root->left->data].push_back(root->data); } if(root->right){ adj[root->data].push_back(root->right->data); adj[root->right->data].push_back(root->data); } traverse(root->left, adj, distance); traverse(root->right, adj, distance); } int findDist(Node* root, int a, int b) { unordered_map<int, vector<int>> adj; unordered_map<int, int> distance; traverse(root, adj, distance); distance[a] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push({0, a}); while(!pq.empty()){ int currDist = pq.top().first; int currNode = pq.top().second; pq.pop(); for(int const &newNode: adj[currNode]){ if(1 + currDist < distance[newNode]){ distance[newNode] = 1 + currDist; pq.push({(1+currDist), newNode}); } } } return distance[b]; } };

GFG | Problem of the day :

class Solution { public: vector> groupAnagrams(vector& strs) { unordered_map > mp; string t; for(int i=0;i> ans; for(auto pr:mp) { ans.push_back(pr.second); } return ans; } };

LeetCode | Daily challenge :

class Solution { public: //Function to return count of nodes at a given distance from leaf nodes. set getNodes(Node* root, int k, int& result, int& leaves){ if(root == nullptr) return {}; if(root -> left == nullptr && root -> right == nullptr) {leaves++; return {0};} set left = getNodes(root -> left, k, result,leaves); set right = getNodes(root -> right, k, result, leaves); set curSet; for(auto it : left) curSet.insert(it+1); for(auto it : right) curSet.insert(it+1); if(curSet.find(k) != curSet.end()) result++; return curSet; } public: //Function to return count of nodes at a given distance from leaf nodes. int printKDistantfromLeaf(Node* root, int k) { int result = 0, leaves = 0; getNodes(root,k,result,leaves); if(k == 0) return leaves; return result; } };

GFG | Problem of the day :

class Solution { public: int firstUniqChar(string s) { int arr[26] = {0}; // Creating a array to map the count of characters for(int i=0;i

LeetCode | Daily challenge :

class Solution { public: Node* sortedInsert(Node* head, int data) { if (head == nullptr) { Node* node = new Node(data); node->next = node; return node; } Node* node = new Node(data), *prev = head, *curr = head->next; while (curr != head) { if (data >= prev->data && data < curr->data) break; Node* temp = curr->next; prev = curr; curr = temp; } prev->next = node; node->next = curr; return curr == head && curr->data > data ? node : head; } };

GFG | Problem of the day :

class Solution { public: string minWindow(string s, string t) { int n = s.size(); unordered_map mp; for(auto &i:t){ mp[i]++; } int count = mp.size(); int i=0; int j=0; int start = 0; int mini = 1e9; while(jj-i+1){ mini = j-i+1; start = i; } mp[s[i]]++; if(mp[s[i]]>0){ count=1; } i++; } } j++; } if(mini != 1e9){ return s.substr(start, mini); } return ""; } };

LeetCode | Daily challenge :

class Solution { public: Node* subLinkedList(Node* head1, Node* head2) { Node * helper = new Node(0); while(head1 && head1->data==0) head1 = head1->next; while(head2 && head2->data==0) head2 = head2->next; if(!head1) return head2 ? head2 : helper; if(!head2) return head1 ? head1 : helper; Node* t1 = head1, *t2 = head2; while(t1 && t2 && t1->data==t2->data){ t1 = t1->next; t2 = t2->next; } bool one = true; if(!t1 or (t2 && t2->data>t1->data)) one = false; int cnt1 = 0, cnt2 = 0; t1 = head1; Node * next, *prev = NULL; while(t1){ next = t1->next; t1->next = prev; prev = t1; t1 = next; ++cnt1; } head1 = prev; prev = NULL, t2 = head2; while(t2){ next = t2->next; t2->next = prev; prev = t2; t2 = next; ++cnt2; } head2 = prev; if(one && cnt2>cnt1) one = false; else if(!one && cnt1>cnt2) one = true; if(!one) swap(head1,head2); Node* head = new Node(-1); Node* t = head; t1 = head1; t2 = head2; int sub = 0; while(t1){ int num = t1->data; if(t2) num -= t2->data; num -= sub; if(num<0){ num += 10; sub = 1; } else { sub = 0; } t->next = new Node(num); t = t->next; t1 = t1->next; if(t2) t2 = t2->next; } head = head->next; prev = NULL; t = head; while(t){ next = t->next; t->next = prev; prev = t; t = next; } head = prev; while(head && head->data==0) head = head->next; return head ? head : helper; } };

GFG | Problem of the day :