uz
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

LeetCode, GeeksForGeeks Problem of the day solution

Kanalga Telegram’da o‘tish

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

Ko'proq ko'rsatish
1 250
Obunachilar
+224 soatlar
+147 kunlar
+2930 kunlar
Postlar arxiv
class Solution { int helper(string ring,string key,int i,int j,int n){ if(j==key.size()) return 0; if(t[i][j]!=-1) return t[i][j]; int r=INT_MAX; for(int a=0;a

LeetCode | Daily challenge :

class Solution { public: // Function to sort the given doubly linked list using Merge Sort. struct Node* merge(struct Node *left, struct Node *right){ struct Node *ans = NULL; if(left->data < right->data){ ans = left; left = left->next; }else{ ans = right; right = right->next; } struct Node *tail = ans; while(left != NULL && right != NULL){ if(left->data < right->data){ struct Node *x = left; tail->next = x; x->prev = tail; tail = tail->next; left = left->next; }else{ struct Node *x = right; tail->next = x; x->prev = tail; tail = tail->next; right = right->next; } } while(left != NULL){ struct Node *x = left; tail->next = x; x->prev = tail; tail = tail->next; left = left->next; } while(right != NULL){ struct Node *x = right; tail->next = x; x->prev = tail; tail = tail->next; right = right->next; } tail->next = NULL; return ans; } struct Node* mergesort(struct Node* head, int n){ if(n <= 1) return head; int mid = (n-1)/2; int curr = 0; struct Node *temp1 = head; while(curr < mid){ temp1 = temp1->next; curr++; } struct Node *temp2 = temp1->next; temp1->next = NULL; temp2->prev = NULL; struct Node *left = mergesort(head, mid+1); struct Node *right = mergesort(temp2, n-mid-1); return merge(left, right); } // Function to sort the given doubly linked list using Merge Sort. struct Node *sortDoubly(struct Node *head) { int n = 0; struct Node *temp = head; while(temp != NULL){ n++; temp = temp->next; } return mergesort(head, n); } };

GFG | Problem of the day :

class Solution { public: int minFallingPathSum(vector<vector<int>>& grid) { int n = grid.size(); vector <vector <int>> dp(n,vector <int> (n,INT_MIN)); priority_queue <pair <int,int>, vector <pair <int,int>>, greater <pair <int,int>>> pq; for(int i=0;i<n;i++){ dp[0][i] = grid[0][i]; pq.push({grid[0][i],i}); } for(int i=1;i<n;i++){ for(int j=0;j<n;j++){ if(pq.top().second==j){ auto x = pq.top(); pq.pop(); dp[i][j] = grid[i][j]+pq.top().first; pq.push(x); }else{ dp[i][j] = grid[i][j]+pq.top().first; } } while(pq.size()) pq.pop(); for(int j=0;j<n;j++){ pq.push({dp[i][j],j}); } } int ans = INT_MAX; while(pq.size()){ ans = min(ans,pq.top().first); pq.pop(); } return ans; } };

LeetCode | Daily challenge :

class Solution { public: vector FindExitPoint(int n, int m, vector>& matrix) { int d=0, i=0, j=0; while(i>=0 && i=0 && j

GFG | Problem of the day :

class Solution { public: int longestIdealString(string s, int k) { int n = s.length(); vector t(26, 0); int result = 0; for(int i=0; i

LeetCode | Daily challenge :

class Solution { public: int findMaxSum(int n, int m, vector> mat) { int ans=-1; for(int i=1;i

GFG | Problem of the day :

class Solution { public: int tribonacci(int n) { if(n==0){ return 0; } if(n==1 || n==2){ return 1; } vector dp(n+1, 0); dp[0] = 0; dp[1] = 1; dp[2] = 1; for(int i=3;i<=n;i++){ dp[i] = dp[i-1] + dp[i-2] + dp[i-3]; } return dp[n]; } };

LeetCode | Daily challenge :

class Solution { public: int mod=1000000007; int ways(int x, int y) { //code here. vector<vector<int>>dp(x+1,vector<int>(y+1,-1)); if(x==0 || y==0) return 1; for(int i=0;i<=x;i++){ dp[i][0]=1; } for(int j=0;j<=y;j++){ dp[0][j]=1; } for(int i=1;i<=x;i++){ for(int j=1;j<=y;j++){ dp[i][j]=dp[i-1][j]%mod+dp[i][j-1]%mod; } } return dp[x][y]%mod; } };

GFG | Problem of the day :

class Solution { public: vector findMinHeightTrees(int n, vector>& edges) { if (n == 1) { return {0}; } unordered_map> graph; vector degrees(n, 0); for (const auto& edge : edges) { int u = edge[0]; int v = edge[1]; graph[u].push_back(v); graph[v].push_back(u); degrees[u]++; degrees[v]++; } queue q; for (int i = 0; i < n; i++) { if (degrees[i] == 1) { q.push(i); } } int remainingNodes = n; while (remainingNodes > 2) { int size = q.size(); remainingNodes -= size; for (int i = 0; i < size; i++) { int leaf = q.front(); q.pop(); for (int neighbor : graph[leaf]) { degrees[neighbor]--; if (degrees[neighbor] == 1) { q.push(neighbor); } } } } vector result; while (!q.empty()) { result.push_back(q.front()); q.pop(); } return result; } };

LeetCode | Daily challenge :

class Solution { public: int firstElement(int n) { int first=1,second=1,ans=0; if(n==1 || n==2) return first; for(int i=0;i

GFG | Problem of the day :