ar
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

LeetCode, GeeksForGeeks Problem of the day solution

الذهاب إلى القناة على Telegram

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

إظهار المزيد
1 250
المشتركون
+224 ساعات
+147 أيام
+2930 أيام
أرشيف المشاركات
LeetCode | Daily challenge :

class twoStacks { int *arr; int size; int top1, top2; public: twoStacks(int n=100) { size = n; arr = new int[n]; top1 = -1; top2 = size; } //Function to push an integer into the stack1. void push1(int x) { top1++; arr[top1] = x; } //Function to push an integer into the stack2. void push2(int x) { top2--; arr[top2] = x; } //Function to remove an element from top of the stack1. int pop1() { if(top1==-1){ return -1; } else{ int val = arr[top1]; top1--; return val; } } //Function to remove an element from top of the stack2. int pop2() { if(top2==size){ return -1; } else{ int val = arr[top2]; top2++; return val; } } };

GFG | Problem of the day :

class Solution { public: bool canFinish(int numCourses, vector>& prerequisites) { int n=numCourses; vector indegree(n, 0); vector> adj(n, vector()); for (auto& pre : prerequisites) { int u = pre[1]; int v = pre[0]; indegree[v]++; adj[u].push_back(v); } queueq; for(int i=0; ians; while(!q.empty()){ int node=q.front(); q.pop(); ans.push_back(node); for(auto it:adj[node]){ indegree[it]--; if(indegree[it]==0) q.push(it); } } if(ans.size()!=n) return 0; else return 1; } };

LeetCode | Daily challenge :

class Solution { public: bool isFrequencyUnique(int n, int arr[]) { unordered_map mp; set st; for(int i=0;i

GFG | Problem of the day :

class Solution{ public: vector eventualSafeNodes(vector>& G) { int N = G.size(); vector> R(N); vector outdegree(N), safe(N), ans; queue q; for (int i = 0; i < N; ++i) { for (int v : G[i]) { R[v].push_back(i); } outdegree[i] = G[i].size(); if (outdegree[i] == 0) q.push(i); } while (q.size()) { int u = q.front(); q.pop(); safe[u] = 1; for (int v : R[u]) { if (--outdegree[v] == 0) q.push(v); } } for (int i = 0; i < N; ++i) { if (safe[i]) ans.push_back(i); } return ans; } };

LeetCode | Daily challenge :

class Solution{ public: //You need to complete this fucntion #define mod 1000000007 long long power(long long a,long long b){ long long ans=1; while(b){ if(b & 1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans; } long long power(int N,int R) { return (power(N,R)%mod); } };

GFG | Problem of the day :

class Solution { public: void markParents(TreeNode* root, unordered_map& parent_track){ queue q2; q2.push(root); while(!q2.empty()){ TreeNode* temp = q2.front(); q2.pop(); if(temp->left){ parent_track[temp->left] = temp; q2.push(temp->left); } if(temp->right){ parent_track[temp->right] = temp; q2.push(temp->right); } } } vector distanceK(TreeNode* root, TreeNode* target, int k) { unordered_map parent_track; markParents(root, parent_track); unordered_map vis; queue q; q.push(target); vis[target] = true; int dist=0; while(!q.empty()){ int size = q.size(); if(dist == k){ break; } dist++; for(int i=0;ileft && !vis[node->left]){ q.push(node->left); vis[node->left] = true; } if(node->right && !vis[node->right]){ q.push(node->right); vis[node->right] = true; } if(parent_track[node] && !vis[parent_track[node]]){ q.push(parent_track[node]); vis[parent_track[node]] = true; } } } vector ans; while(!q.empty()){ TreeNode* temp=q.front(); q.pop(); ans.push_back(temp->val); } return ans; } };

LeetCode | Daily challenge :

class Solution { public: /*You are required to complete this method*/ int findK(int a[MAX][MAX],int n,int m,int k) { vectorans; int rowStart =0; int rowEnd = n-1; int colStart = 0; int colEnd = m-1; while(rowStart <= rowEnd && colStart<=colEnd){ //left to right for(int i=colStart; i<=colEnd; i++){ ans.push_back(a[rowStart][i]); } rowStart++; //top to bottom for(int i=rowStart; i<=rowEnd; i++){ ans.push_back(a[i][colEnd]); } colEnd--; if(rowStart<=rowEnd){ for(int i=colEnd; i>=colStart; i--){ ans.push_back(a[rowEnd][i]); } rowEnd--; } if(colStart <= colEnd){ for(int i=rowEnd; i>=rowStart; i--){ ans.push_back(a[i][colStart]); } colStart++; } } return ans[k-1]; } };

GFG | Problem of the day :

class Solution { public: int minDepth(TreeNode* root) { if(root==NULL){ return 0; } if(root->left==NULL && root->right==NULL){ return 1; } int leftST = 1e9; if(root->left){ leftST = minDepth(root->left); } int rightST = 1e9; if(root->right){ rightST = minDepth(root->right); } return 1+min(leftST, rightST); } };

LeetCode | Daily challenge :

class Solution { public: //Function to find transpose of a matrix. void transpose(vector >& matrix, int n) { for(int i=0;i

GFG | Problem of the day :

class Solution { public: int largestVariance(string s) { vectorarr(26); for(auto w:s){ arr[w-'a']++; } int ans=0; for(char i='a';i<='z';i++){ for(char j='a';j<='z';j++){ if(j==i or arr[i-'a']==0 or arr[j-'a']==0) continue; for(int k=1;k<=2;k++){ int c1=0; int c2=0; for(auto w:s){ if(w==i) c1++; if(w==j) c2++; if(c2>c1){ c1=0; c2=0; } if(c1>0&&c2>0) ans=max(ans,c1-c2); } reverse(s.begin(),s.end()); } } } return ans; } };