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: /*You are required to complete below method */ void inorder(Node *root,int &sum) { if(root==NULL) return ; if(root->left==NULL && root->right==NULL) { sum=sum+root->data; return ;} inorder(root->left,sum); inorder(root->right,sum); } public: int sumOfLeafNodes(Node *root ){ int sum=0; inorder(root,sum); return sum; } };

GFG | Problem of the day :

class Solution { public: int furthestBuilding(vector<int>& heights, int bricks, int ladders) { priority_queue<int, vector<int>, greater<int>> pq; for(int i = 1; i < heights.size(); i++) { int diff = heights[i] - heights[i-1]; if(diff > 0) pq.push(diff); if(pq.size() > ladders) { if(pq.top() <= bricks) bricks -= pq.top(), pq.pop(); else return i-1; } } return heights.size()-1; } };

LeetCode | Daily challenge :

class Solution{ public: bool isMaxHeap(int arr[], int n) { for(int i=0;i<=(n-2)/2;i++) { if(arr[2*i+1] > arr[i]) { return false; } else if(2*i+2<n && arr[2*i+2] > arr[i]) { return false; } } return true; } };

GFG | Problem of the day :

class Solution { public: int findLeastNumOfUniqueInts(vector& arr, int k) { unordered_mapmp; for(auto it:arr) mp[it]++; vector>dp; for(auto it:mp){ dp.push_back(it); } sort(begin(dp),end(dp),[](auto a,auto b){ return a.second=it.second){ k-=it.second; count++; }else{ break; } } return size(dp)-count; } };

LeetCode | Daily challenge :

class Solution { public: void helper(Node *root,Node * &newNode,Node * &r){ if(!root) return ; helper(root->left,newNode,r); if(!newNode) newNode = root; if(r){ r->right = root; r->left = NULL; } r = root; helper(root->right,newNode,r); } public: Node *flattenBST(Node *root) { Node * newNode = NULL,*r = NULL; helper(root,newNode,r); r->left = NULL; return newNode; } };

GFG | Problem of the day :

class Solution { public: long long largestPerimeter(vector& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); long long ans = 0, cur = 0; for(int i=0; i nums[i] && i >= 2){ ans = max(cur, ans); ans += nums[i]; } cur += nums[i]; } if(ans == 0) return -1; return ans ; } };

LeetCode | Daily challenge :

class Solution { public: int isPossible(vector>paths){ for(int i=0;i

GFG | Problem of the day :

class Solution { public: long long largestPerimeter(vector& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); long long ans = 0, cur = 0; for(int i=0; i nums[i] && i >= 2){ ans = max(cur, ans); ans += nums[i]; } cur += nums[i]; } if(ans == 0) return -1; return ans ; } };

LeetCode | Daily challenge :

class Solution { public: vector rearrangeArray(vector& nums) { vector positive; vector negative; vector ans; int n = nums.size(); for(int i = 0; i < n; i++){ if(nums[i] < 0){ negative.push_back(nums[i]); } else{ positive.push_back(nums[i]); } } for(int i = 0; i < n/2; i++){ ans.push_back(positive[i]); ans.push_back(negative[i]); } return ans; } };

LeetCode | Daily challenge :

class Solution { public: void dfs(int node , int parent , vector&low ,vector&tin , vector&vis , vector adj[] , int &timer, vector> & bridges ){ vis[node] = 1; low[node] = timer; tin[node] = timer; timer++; for(auto nbr : adj[node]){ if(nbr == parent) continue; else if(!vis[nbr]){ dfs(nbr,node , low , tin , vis , adj , timer , bridges); low[node] = min(low[node] , low[nbr]); if(low[nbr] > tin[node]){ if(nbr> node) bridges.push_back({node,nbr}); else bridges.push_back({nbr,node}); } } else{ low[node] = min(low[node] , low[nbr]); } } } vector>criticalConnections(int v, vector adj[]){ // Code here vector> bridges; vector low(v,0); vector tin(v,0); vector vis(v,0); int parent = -1; int timer = 0; dfs(0 , parent , low , tin , vis , adj , timer , bridges); sort(bridges.begin() , bridges.end()); return bridges; } };

GFG | Problem of the day :