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 أيام
أرشيف المشاركات
class Solution { public: vector frequencySort(vector& nums) { unordered_map mpp;//element-->frequency int n = nums.size(); for(int i=0;i> newMap; for(auto x:mpp){ newMap.push_back({x.first,x.second}); } sort(newMap.begin(),newMap.end(),[](pair &a,pair &b){ if(a.second == b.second){ return a.first>b.first; } else{ return a.second < b.second; } }); vector ans; for(auto x:newMap){ for(int i=1;i<=x.second;i++){ ans.push_back(x.first); } } return ans; } };

LeetCode | Daily challenge :

class Solution { public: // Function to return a list of integers denoting the node // values of both the BST in a sorted order. void solve(Node *root, vector &ans) { if (!root) return; solve(root->left, ans); ans.push_back(root->data); solve(root->right, ans); } vector merge(Node *root1, Node *root2) { vector ans; solve(root1, ans); solve(root2, ans); sort(ans.begin(), ans.end()); return ans; } };

GFG | Problem of the day :

class Solution { public: vector sortPeople(vector& names, vector& heights) { unordered_map map; for(int i=0;i

LeetCode | Daily challenge :

class NodeValue{ public: int minVal, maxVal, maxSize; NodeValue(int minVal, int maxVal, int maxSize){ this->minVal = minVal; this->maxVal = maxVal; this->maxSize = maxSize; } }; class Solution{ public: /*You are required to complete this method */ // Return the size of the largest sub-tree which is also a BST NodeValue largestBSTHelper(Node* root){ if(root==NULL){ return NodeValue(INT_MAX, INT_MIN, 0); } NodeValue leftST = largestBSTHelper(root->left); NodeValue rightST = largestBSTHelper(root->right); if(root->data > leftST.maxVal && root->data < rightST.minVal){ return NodeValue(min(root->data, leftST.minVal), max(root->data, rightST.maxVal), 1+leftST.maxSize+rightST.maxSize); } return NodeValue(INT_MIN, INT_MAX, max(leftST.maxSize, rightST.maxSize)); } int largestBst(Node *root) { return largestBSTHelper(root).maxSize; } };

GFG | Problem of the day :

class Solution { public: void top_sort(unordered_map> &adj,vector &res,int &k) { queue q; vector indegree(k+1,0); for(auto x:adj) { for(auto y:x.second) { indegree[y]++; } } for(int i=1;i<=k;i++) { if(indegree[i]==0) { q.push(i); } } while(!q.empty()) { int front=q.front(); q.pop(); res.push_back(front); for(auto x:adj[front]) { indegree[x]--; if(indegree[x]==0) { q.push(x); } } } } vector> buildMatrix(int k, vector>& rowConditions, vector>& colConditions) { int i,m=rowConditions.size(),n=colConditions.size(); unordered_map> adj1; unordered_map> adj2; for(i=0;i x; vector y; top_sort(adj1,x,k); top_sort(adj2,y,k); vector> ans(k,vector (k,0)); if(x.size()!=k || y.size()!=k) { return {}; } i=0; unordered_map hash; for(auto p:x) { hash[p]=i++; } i=0; for(auto p:y) { ans[hash[p]][i]=p; i++; } return ans; } };

LeetCode | Daily challenge :

class Solution { public: int mod = 1e9+7; long long int findMaxProduct(vector& arr) { long long int p=1; long long int zerocount = 0, negativecount=0; long long int maxin = INT_MIN; if(arr.size()==1) return arr[0]; for(int i=0;i

GFG | Problem of the day :

class Solution { public: vector> restoreMatrix(vector& rowSum, vector& colSum) { int m=rowSum.size(),n=colSum.size(),i=0,j=0; vector>res(m,vector(n,0)); while(i

LeetCode | Daily challenge :

class Solution { public: void inorder(Node* root){ if(root==NULL){ return; } inorder(root->left); cout<data<<" "; inorder(root->right); } Node *RemoveHalfNodes(Node *root) { // code here if(root==NULL){ return NULL; } root->left=RemoveHalfNodes(root->left); root->right=RemoveHalfNodes(root->right); if(root->left==NULL&&root->right!=NULL){ Node* newRoot=root->right; delete root; return newRoot; } if(root->right==NULL&&root->left!=NULL){ Node* newRoot=root->left; delete root; return newRoot; } return root; } };

GFG | Problem of the day :

class Solution { public: vector luckyNumbers (vector>& matrix) { int rows = matrix.size(); int cols = matrix[0].size(); vector row_minimums(rows, INT_MAX); vector col_maximums(cols, 0); for (int row_ind = 0; row_ind < rows; ++row_ind) { for (int col_ind = 0; col_ind < cols; ++col_ind) { int el = matrix[row_ind][col_ind]; row_minimums[row_ind] = min(row_minimums[row_ind], el); col_maximums[col_ind] = max(col_maximums[col_ind], el); } } for (int row_ind = 0; row_ind < rows; ++row_ind) { for (int col_ind = 0; col_ind < cols; ++col_ind) { int el = matrix[row_ind][col_ind]; if (el == row_minimums[row_ind] && el == col_maximums[col_ind]) { return {el}; } } } return {}; } };

LeetCode | Daily challenge :

class Solution{ public: vector constructLowerArray(vectorarr) { // code here vector ans; int n= arr.size(); vector temp; for(int i=0;i

GFG | Problem of the day :