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
GFG | Problem of the day :

class Solution { public: vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) { vector<int>ans(people.size(),0); sort(flowers.begin(),flowers.end()); vector<pair<int,int>>p; for(int i=0;i<people.size();i++){ p.push_back({people[i],i}); } sort(p.begin(),p.end()); int j=0; int cnt=0; priority_queue<int,vector<int>,greater<int>>q; for(auto i:p){ int ele=i.first; while(j<flowers.size() && flowers[j][0]<=ele){ q.push(flowers[j][1]); j++; } while(!q.empty() && q.top()<ele){ q.pop(); } ans[i.second]=q.size(); } return ans; } };

LeetCode | Daily challenge :

class Solution{ public: //Function to check whether a binary tree is balanced or not. pair isBalancedFast(Node* root){ if(root==NULL){ pair p = make_pair(true,0); return p; } pair left = isBalancedFast(root->left); pair right = isBalancedFast(root->right); bool op1 = left.first; bool op2 = right.first; bool op3 = abs(left.second - right.second) <= 1; pair ans; if(op1 && op2 && op3){ ans.first = true; } else{ ans.first = false; } ans.second = max(left.second , right.second) + 1; return ans; } bool isBalanced(Node *root) { return isBalancedFast(root).first; } };

GFG | Problem of the day :

class Solution { public: int minOperations(vector& nums) { if(nums.size()==1) return 0; sort(nums.begin(),nums.end()); int actual=nums.size(); nums.erase(unique(begin(nums),end(nums)),end(nums)); int mini=INT_MAX; for(int i=0;i

LeetCode | Daily challenge :

class Solution { private: public: void atLevelK(Node* root, int k, vector &ans){ if(root){ if(k==0) ans.push_back(root->data); atLevelK(root->left,k-1,ans); atLevelK(root->right,k-1,ans); } } int helper(Node* root, int k, vector &ans, int target, bool &t){ if(root==NULL) return -1; if(!t and root->data==target){ atLevelK(root,k,ans); t = true; return 1; } int l = helper(root->left,k,ans,target,t); int r = helper(root->right,k,ans,target,t); if(l==-1 and r==-1) return -1; else if(r==-1){ if(k-l==0){ ans.push_back(root->data); return -1; } else atLevelK(root->right,k-l-1,ans); return l+1; } else{ if(k-r==0){ ans.push_back(root->data); return -1; } else atLevelK(root->left,k-r-1,ans); return r+1; } } public: vector KDistanceNodes(Node* root, int target , int k){ vector ans; bool t = false; helper(root,k, ans,target,t); sort(ans.begin(),ans.end()); return ans; } };

GFG | Problem of the day :

class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int num1 = -1; int num2 = -1; int s=0; int e=nums.size()-1; while(s<=e){ int mid = s+(e-s)/2; if(nums[mid]==target){ num1 = mid; e--; } else if(nums[mid]>target){ e = mid-1; } else{ s = mid+1; } } s=0; e=nums.size()-1; while(s<=e){ int mid = s+(e-s)/2; if(nums[mid]==target){ num2 = mid; s++; } else if(nums[mid]>target){ e = mid-1; } else{ s = mid+1; } } return {num1, num2}; } };

LeetCode | Daily challenge :

class Solution{ public: //Function to find the height of a binary tree. int height(struct Node* node){ if(node==NULL){ return 0; } return 1+max(height(node->left), height(node->right)); } };

GFG | Problem of the day :

class Solution { public: int maxDotProduct(vector& nums1, vector& nums2) { vector>> v(2, vector> (nums1.size(), vector(nums2.size(), INT_MAX))); return dfs(nums1, nums2, 0, 0, v, false); } int dfs(vector& nums1, vector& nums2, int i, int y, vector>>& v, bool isGood){ if (i == nums1.size() || y == nums2.size()) return isGood ? 0 : INT_MIN; if (v[isGood][i][y] != INT_MAX) return v[isGood][i][y]; v[isGood][i][y] = nums1[i] * nums2[y] + dfs(nums1, nums2, i + 1, y + 1, v, true); int s = dfs(nums1, nums2, i + 1, y, v, isGood); int t = dfs(nums1, nums2, i, y + 1, v, isGood); return v[isGood][i][y] = max(v[isGood][i][y], max(s, t)); } };

LeetCode | Daily challenge :

class Solution{ public: // Should return head of the modified linked list Node *sortedInsert(struct Node* head, int data) { Node * newnode= new Node(data); newnode->next = head; Node* temp = newnode; if(head->data >= data){ head = newnode; } else{ while(temp!=NULL and temp->next!=NULL and temp->next->data <= data){ temp = temp->next; } newnode->next=temp->next; temp->next = newnode; } return head; } };

GFG | Problem of the day :

class Solution { public: int dp[51][101][51]; int solve(int index , int biggest , int cost ,int n ,int k ,int m) { if(index == n) return cost == k; if( cost > k or k-cost > m - biggest or k-cost > n -index) return 0; if(dp[index][biggest][cost] != -1) return dp[index][biggest][cost]; int res= 0; for(int i= 1 ; i<= m ; i++) res = (res + solve(index+1 , max(biggest , i) , i>biggest? cost + 1 : cost,n,k,m))%1000000007; return dp[index][biggest][cost] = res; } int numOfArrays(int n, int m, int k) { memset(dp , -1 , sizeof(dp)); return solve(0, 0 , 0 ,n ,k ,m); } };

LeetCode | Daily challenge :

class Solution { public: Node* pairWiseSwap(struct Node* head) { Node*prev=NULL; Node*curr=head; Node*forward=NULL; int cnt=0; while(curr!=NULL && cnt<2){ forward=curr->next; curr->next=prev; prev=curr; curr=forward; cnt++; } if(forward!=NULL){ head->next=pairWiseSwap(forward); } return prev; } };