uk
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: int maxSubarrayLength(vector& nums, int k) { int i = 0, j = 0; map mp; int res = 0; while(j < nums.size()){ mp[nums[j]]++; while(mp[nums[j]] > k && i < j){ mp[nums[i]]--; i++; } res = max(res, j - i + 1); j++; } return res; } };

LeetCode | Daily challenge :

class Solution { public: int findCity(int n, int m, vector<vector<int>>& edges, int dt) { // Your code here vector<pair<int,int>>adj[n]; for(int i=0;i<edges.size();i++) { adj[edges[i][0]].push_back({edges[i][1],edges[i][2]}); adj[edges[i][1]].push_back({edges[i][0],edges[i][2]}); } vector<pair<int,int>>v; int maxi = INT_MAX; int result = -1; for(int i=0;i<n;i++) { priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>>>q; vector<int>visited(n,1e9); visited[i]=0; q.push({i,0}); int count=0; while(!q.empty()) { pair<int,int>ll=q.top(); int h=ll.first; int k=ll.second; q.pop(); for(auto it:adj[h]) { if( k+it.second<visited[it.first]) { visited[it.first]=k+it.second; q.push({it.first,k+it.second}); } } } for(int j = 0; j < visited.size(); j++){ if(visited[j] <= dt){ count++; } } if(count <= maxi){ maxi = count; result = i; } } return result; } };

GFG | Problem of the day :

class Solution { public: int numSubarrayProductLessThanK(vector& nums, int k) { if(k <= 1) return 0; int n = nums.size(); int count = 0; int left = 0; int right = 0; int prod = 1; while(right < n){ prod *= nums[right]; while(prod >= k) { prod /= nums[left]; left++; } count += (right-left)+1; right++; } return count; } };

LeetCode | Daily challenge :

class Solution { public: int findShortestPath(vector> &mat) { int r = mat.size(), c = mat[0].size(); int dir[5] = {-1, 0, 1, 0, -1}; for(int i=0; i= 0 and x < r and y >= 0 and y < c and mat[x][y] == 1) mat[x][y] = 2; } } } } vector> visited(r, vector (c, false)); queue> Q; for(int i=0; i= 0 and x < r and y >= 0 and y < c and mat[x][y] == 1 and !visited[x][y]) { Q.push({x,y}); visited[x][y] = true; } } } level++; } return -1; } };

GFG | Problem of the day :

class Solution { public: int firstMissingPositive(vector& A) { int n = A.size(); for(int i = 0;i < n;i++) { while(A[i] > 0 and A[i] <= n and A[A[i] - 1] != A[i]) { swap(A[i] , A[A[i] - 1]); } } for(int i = 0;i < n;i++) { if(A[i] != i + 1) { return i + 1; } } return n + 1; } };

LeetCode | Daily challenge :

class Solution { public: bool valid(string &n,int k,int num1,int num2){ for(int i=k;i

GFG | Problem of the day :

class Solution { public: vector findDuplicates(vector& nums) { vector res; map mp; for(int i=0;i

LeetCode | Daily challenge :

class Solution{ public: void find(vector<string>& s, string temp, int n,int count1, int index) { if(count1<(temp.size()+1)/2) return; if(index==n){ s.push_back(temp); return; } find(s, temp+'1', n, count1+1, index+1); find(s, temp+'0', n, count1, index+1); } vector<string> NBitBinary(int n) { // Your code goes here vector<string> s; find(s,"",n,0,0); return s; } };

GFG | Problem of the day :

class Solution { public: int findDuplicate(vector<int>& nums) { while(nums.at(0) != nums.at(nums.at(0))){ swap(nums.at(0), nums.at(nums.at(0))); } return nums.at(0); } };

LeetCode | Daily challenge :

class Solution{ public: stack<int> insertAtBottom(stack<int> st,int x){ vector<int> temp; while(!st.empty()){ temp.push_back(st.top()); st.pop(); } st.push(x); for(int i=temp.size()-1; i>=0; i--){ st.push(temp[i]); } return st; } };

GFG | Problem of the day :