ch
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
帖子存档
GFG | Problem of the day :

class Solution { public: vector> updateMatrix(vector>& mat) { int m = mat.size(); int n = mat[0].size(); vector> dist(m , vector(n)); vector> vis(m, vector(n, 0)); queue, int>> q; for(int i=0;i=0 && nr=0 && nc

LeetCode | Daily challenge :

class Solution{ public: vector<int> generateNextPalindrome(int num[], int n) { int l = 0; int u = n - 1; bool greater = false; while(l <= u) { if(num[l] > num[u]) greater = true; else if(num[l] < num[u]) greater = false; num[u] = num[l]; ++l; --u; } l = (n-1)/2; u = n/2; while(!greater && l>=0 && u<n) { if(num[l] == 9) num[l] = num[u] = 0; else { ++num[l]; if(l != u) ++num[u]; break; } --l; ++u; } vector<int> result; if(num[0] == 0) result.push_back(1); for(int i=0; i<n; i++) { if(num[0] == 0 && i == 0) continue; result.push_back(num[i]); } if(num[0] == 0) result.push_back(1); return result; } };

GFG | Problem of the day :

class Solution { public: vector maxSlidingWindow(vector& nums, int k) { int n=nums.size(); deque dq; int i=0; vector ans; for(int j=0;j

LeetCode | Daily challenge :

class Solution { public: //Function to find the nth catalan number. #define mod 1000000007 int findCatalan(int n) { vector<long long int> dp(n+1); dp[0] = dp[1] = 1; for(int i = 2; i <= n; i++){ dp[i] = 0; for(int j = 0; j < i; j++){ dp[i] = (dp[i] + dp[j] * dp[i-j-1])%mod; } } return dp[n]; } };

GFG | Problem of the day :

class Solution { public: ListNode* partition(ListNode* head, int z) { ListNode* a = nullptr; ListNode* b = nullptr; ListNode* a_start = nullptr; ListNode* b_start = nullptr; int an = 0, bn = 0; ListNode* x = head; while (x != nullptr) { if (x->val < z) { ListNode* t = new ListNode(x->val); if (an == 0) { a_start = t; a = t; } else { a->next = t; a = t; } an++; } else { ListNode* t = new ListNode(x->val); if (bn == 0) { b_start = t; b = t; } else { b->next = t; b = t; } bn++; } x = x->next; } if (an == 0) { return b_start; } if (bn == 0) { return a_start; } a->next = b_start; return a_start; } };

LeetCode | Daily challenge :

class Solution{ public: int maxOnes(int a[], int n) { int one=0; for(int i=0; i

GFG | Problem of the day :

class Solution { public: int findKthLargest(vector<int>& nums, int k) { priority_queue<int, vector<int> , greater<int>> pq; for(int i=0;i<nums.size();i++){ pq.push(nums[i]); } while(pq.size()>k){ pq.pop(); } return pq.top(); } };

LeetCode | Daily challenge :

class Solution { public: vector singleNumber(vector nums) { map mp; for(auto it:nums){ mp[it]++; } vector ans; for(auto it:mp){ if(it.second==1){ ans.push_back(it.first); } } return ans; } };

GFG | Problem of the day :

class Solution { public: unordered_map dp; bool solve(vector &nums, int i){ int n = nums.size(); // Edge cases if(i > n) return false; if(i == n) return true; // memoization if(dp.find(i) != dp.end()) return dp[i]; // if only one value in remaning return false if(i + 1 == n) return false; // else check for i + 1 index is valid if(nums[i] == nums[i + 1] && solve(nums, i + 2)) return true; // check for i + 2 index's validation if(i + 2 == n) return false; // check for condition 2 if(nums[i] == nums[i + 1] && nums[i] == nums[i + 2] && solve(nums, i + 3)) return true; // check for condition 3 if(nums[i] + 1 == nums[i + 1] && nums[i + 1] + 1 == nums[i + 2] && solve(nums, i + 3)) return true; // memorize the result while returning return dp[i] = false; } bool validPartition(vector& nums) { return solve(nums, 0); } };

LeetCode | Daily challenge :

class Solution { public: #define mod 1000000007 int nthFibonacci(int n){ vector<int> dp(n+1, 0); dp[0] = 0; dp[1] = 1; for(int i=2;i<=n;i++){ dp[i] = (dp[i-1] + dp[i-2])%mod ; } return dp[n]%mod; } };