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 openLock(vector& deadends, string target) { int pow10[] = {1, 10, 100, 1000}; int visit[10000] = {0}; for(string dead : deadends) { visit[stoi(dead)] = 2; } int src = 0, dest = stoi(target), steps = 0, dir = 1; if(visit[src] == 2 || visit[dest] == 2) return -1; if(src == dest) return 0; queue forward, backward; forward.push(src); visit[src] = 1; backward.push(dest); visit[dest] = -1; while(!forward.empty() && !backward.empty()) { if(forward.size() > backward.size()) { swap(forward, backward); dir = -dir; } steps++; int size = forward.size(); while(size-- > 0) { int cur = forward.front(); forward.pop(); for(int p : pow10) { int d = (cur / p) % 10; for(int i = -1; i <= 1; i += 2) { int z = d + i; z = z == -1 ? 9 : (z == 10 ? 0 : z); int next = cur + (z - d) * p; if(visit[next] == -dir) return steps; if(visit[next] == 0) { forward.push(next); visit[next] = dir; } } } } } return -1; } };

LeetCode | Daily challenge :

class Solution { public: int minRow(int n, int m, vector> a) { int ans = INT_MAX; int cnt = 1; int res; for(auto iter : a){ int val = count(iter.begin(),iter.end(),1); if(val < ans){ ans = val; res = cnt; } cnt++; } return res; } };

GFG | Problem of the day :

class Solution { public: bool validPath(int n, vector>& edges, int source, int destination) { if(edges.size()==0) return true; vector adj[n]; for(auto &it : edges){ adj[it[0]].push_back(it[1]); adj[it[1]].push_back(it[0]); } vector vi(n, 0); queue q; q.push(source); while(!q.empty()){ int node=q.front(); q.pop(); for(auto &it : adj[node]){ if(vi[it]==0){ if(it==destination) return true; vi[it]=1; q.push(it); } } } return false; } };

LeetCode | Daily challenge :

class Solution{ public: //Function to partition the array around the range such //that array is divided into three parts. void threeWayPartition(vector& array,int a, int b) { int i=0,j=array.size()-1; while(ib){ swap(array[i],array[j]); j--; } else i++; } while(array[j]>b){ j--; } i=0; while(i=a){ swap(array[i],array[j]); j--; } else i++; } } };

GFG | Problem of the day :

class Solution { public: void dfs(int i,int j,int &sr,int &sc,int &er,int &ec,vector>& land){ if(i<0 or j<0 or i==land.size() or j==land[0].size() or land[i][j]!=1) return; land[i][j]=0; sr=min(sr,i); sc=min(sc,j); er=max(er,i); ec=max(ec,j); dfs(i+1,j,sr,sc,er,ec,land); dfs(i-1,j,sr,sc,er,ec,land); dfs(i,j+1,sr,sc,er,ec,land); dfs(i,j-1,sr,sc,er,ec,land); } vector> findFarmland(vector>& land) { vector> ans; for(int i=0;i

LeetCode | Daily challenge :

class Solution{ public: //arr1,arr2 : the arrays // n, m: size of arrays //Function to return a list containing the union of the two arrays. vector findUnion(int arr1[], int arr2[], int n, int m) { unordered_setx; vectorv; for(int i=0;i

GFG | Problem of the day :

class Solution { public: bool isValid(int i, int j, int rows, int cols){ if(i<0 or j<0 or i==rows or j==cols){ return false; } return true; } void dfs(vector>& grid, int i, int j, int rows, int cols){ if(!isValid(i, j, rows, cols)){ return; } if(grid[i][j]=='0'){ return; } grid[i][j]='0'; dfs(grid, i+1, j, rows, cols); dfs(grid, i, j+1, rows, cols); dfs(grid, i-1, j, rows, cols); dfs(grid, i, j-1, rows, cols); } int numIslands(vector>& grid) { int rows = grid.size(); int cols = grid[0].size(); int cnt=0; for(int i=0;i

LeetCode | Daily challenge :

class Solution{ public: vector findMissing(int a[], int b[], int n, int m) { // Your code goes here map mp; for(int i=0; i v; for(int i=0; i

GFG | Problem of the day :

class Solution { public: int cnt = 0; int vis[101][101]; void dfs(int i, int j, vector>& grid) { if(i >= grid.size() or j >= grid[0].size() or i < 0 or j < 0 or grid[i][j] == 0) { cnt++; return; } if(vis[i][j]) { return; } vis[i][j] = 1; dfs(i+1, j, grid); dfs(i-1, j, grid); dfs(i, j+1, grid); dfs(i, j-1, grid); } int islandPerimeter(vector>& grid) { for(int i=0; i

LeetCode | Daily challenge :

class Solution { public: //Function to find two repeated elements. vector twoRepeated (int arr[], int n) { int x=arr[n+1],a=0,b=0; for(int i=0;i<=n;i++) x^=arr[i]^i; for(int i=0;i<=n+1;i++) { if(arr[i]&(x&-x))a^=arr[i]; else b^=arr[i]; if(i<=n) { if(i&(x&-x))a^=i; else b^=i; } } for(int i=n+1;i>=0;i--) { if(a==arr[i]) { return {b,a}; } else if(b==arr[i]) return {a,b}; } return {}; } };

GFG | Problem of the day :