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 день
Архів дописів
LeetCode | Daily challenge :

class Solution{ public: int countSubArrayProductLessThanK(const vector& a, int n, long long k) { int ans = 0; int lt = 0; int rt = 0; long long curr = 1; while(rt=k){ // ans+=(rt-lt); lt = rt; rt++; lt++; curr = 1; } else if(curr*a[rt]=k){ curr = curr/a[lt]; lt++; if(lt==rt){ curr = 1; break; } } } } return ans; } };

GFG | Problem of the day :

class Solution { public: bool buddyStrings(string s, string goal) { if (s.length() != goal.length()) { return false; } vector<int> diff_indices; vector<char> diff_chars; for (int i = 0; i < s.length(); i++) { if (s[i] != goal[i]) { diff_indices.push_back(i); diff_chars.push_back(s[i]); } } if (diff_indices.size() == 0) { // If both strings are equal, check if there are duplicate characters in s vector<int> count(26, 0); for (char c : s) { count[c - 'a']++; if (count[c - 'a'] > 1) { return true; } } return false; } if (diff_indices.size() != 2) { return false; } int i = diff_indices[0]; int j = diff_indices[1]; return (s[i] == goal[j] && s[j] == goal[i]); } };

LeetCode | Daily challenge :

class Solution{ public: int maxIndexDiff(int arr[], int n) { int nums[n]; for(int i =0;i

GFG | Problem of the day :

class Solution { public: void solve(int n , int ind , vector > &requests , vector &indeg , int &ans , int count){ if(ind == requests.size()){ int flag =1; for(int i =0 ; i < n ;i++){ if(indeg[i] != 0){ flag =0 ; break; } } if(flag){ ans = max(ans , count ); } return; } solve(n ,ind+1 , requests , indeg , ans , count); indeg[requests[ind][0]]--; indeg[requests[ind][1]]++; solve(n ,ind+1 , requests , indeg , ans,count+1); indeg[requests[ind][0]]++; indeg[requests[ind][1]]--; } int maximumRequests(int n, vector>& requests) { int ans = INT_MIN; vector indeg(n , 0); solve(n ,0, requests, indeg , ans , 0); return ans ; } };

LeetCode | Daily challenge :

class Solution{ public: int setSetBit(int x, int y, int l, int r){ for(int i{l-1};i<=(r-1);i++){ if(y & (1 << i)) x |= (1 << i); } return x; } };

GFG | Problem of the day :

class Solution { public: vectorbucket; int ans; void backtracking(vector&cookies,int k,int cookieNumber) { if(cookieNumber==cookies.size()) { int maxx=0; for(int i=0;i& cookies, int k) { bucket.resize(k,0); ans=INT_MAX; backtracking(cookies, k, 0); return ans; } };

LeetCode | Daily challenge :

class Solution { public: int setBits(int N) { int count=0; for(int i=31;i>=0;i--){ int bit=(N>>i) & 1; if(bit==1){ count++; } } return count; } };

GFG | Problem of the day :

class Solution { public: int latestDayToCross(int row, int col, vector>& cells) { int N = row + 5, M = col + 5; int a[N][M], dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}; bool vis[N][M]; // set date to each cell for(int i = 0; i < cells.size(); i++) a[cells[i][0]][cells[i][1]] = i+1; // binary search int l = 0, r = row * col; while(l < r){ int mid = (l+r+1) / 2; bool check = false; fill_n(vis[0], N*M, false); queue> q; // push the top cells for(int i = 1; i <= col; i++) if(a[1][i] > mid) q.push({1,i}); // BFS while(!q.empty()){ auto [x, y] = q.front(); q.pop(); if(vis[x][y]) continue; vis[x][y] = true; // check if a current cell is the bottom if(x == row){ check = true; break; } for(int i = 0; i < 4; i++){ int xx = x + dx[i], yy = y + dy[i]; if(xx > row or xx < 1 or yy < 1 or yy > col or vis[xx][yy] or a[xx][yy] <= mid) continue; q.push({xx,yy}); } } if(check) l = mid; else r = mid-1; } return r; } };

LeetCode | Daily challenge :

class Solution{ public: int isDivisible(string s){ int c=0; int x=1; for(int i=s.size()-1;i>=0;i--) { if(s[i]=='1') c+=x; x=(x==1)?2:1; } return (c%3==0)?1:0; } };

GFG | Problem of the day :

class Solution { public: const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; int shortestPathAllKeys(vector& grid) { int m = grid.size(); int n = grid[0].size(); int keys=0; queue> q; for(int i=0; i= 'a' && grid[i][j] <= 'z') { keys++; } if(grid[i][j]=='@') { q.push({i,j,0}); // {i,j,mask} } } } // The main crux of the problem is that we are going to visit the same cell again only if we are visiting the cell with different keysState, // otherwise we will be in a infinite loop as we can vis the cells again, it's not like simple BFS, it's BFS WITH STATES. set,int>> vis; // {i,j,"currKeys"} int steps = 0; while(!q.empty()){ int queueSize = q.size(); for(int i=0; i= 'a' && grid[nX][nY] <= 'z') { newMask |= (1 << (grid[nX][nY] - 'a')); } if(vis.find({{nX, nY}, newMask}) != vis.end() || (grid[nX][nY] >= 'A' && grid[nX][nY] <= 'Z' && !(mask&(1<<(grid[nX][nY]-'A'))))) { continue; } q.push({nX, nY, newMask}); vis.insert({{nX, nY}, newMask}); } } steps++; } return -1; } };