-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
Day 59/Geek's Village and Wells_GFG/Geek's_Village_and_Wells.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Solution{ | ||
public: | ||
vector<vector<int>> chefAndWells(int n,int m,vector<vector<char>> &c){ | ||
// Code here | ||
vector<vector<int>> vis(n,vector<int>(m,0)); | ||
vector<vector<int>> ans(n,vector<int>(m,-1)); | ||
queue<pair<int,pair<int,int>>> q; | ||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<m;j++) | ||
{ | ||
if(c[i][j]=='N') { | ||
ans[i][j]=0; | ||
vis[i][j]=1; | ||
} | ||
if(c[i][j]=='W'){ | ||
vis[i][j]=1; | ||
q.push({0,{i,j}}); | ||
ans[i][j]=0; | ||
} | ||
if(c[i][j]=='.') ans[i][j]=0; | ||
} | ||
} | ||
int dx[4] = {0,0,1,-1}; | ||
int dy[4] = {1,-1,0,0}; | ||
while(!q.empty()) | ||
{ | ||
int row = q.front().second.first; | ||
int col = q.front().second.second; | ||
int dis = q.front().first; | ||
q.pop(); | ||
for(int i=0;i<4;i++) | ||
{ | ||
int newi = row + dx[i]; | ||
int newj = col + dy[i]; | ||
if(newi>=0&&newi<n&&newj>=0&&newj<m&&vis[newi][newj]==0&&(c[newi][newj]=='H'||c[newi][newj]=='.')) | ||
{ | ||
if(c[newi][newj]=='H') ans[newi][newj] = 2*(dis+1); | ||
q.push({dis+1,{newi,newj}}); | ||
vis[newi][newj]=1; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
Day 59/Similar String Groups_Leetcode/Similar_String_Groups.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Solution { | ||
public: | ||
int numSimilarGroups(vector<string>& A) { | ||
int ans = 0; | ||
vector<bool> seen(A.size()); | ||
|
||
for (int i = 0; i < A.size(); ++i) | ||
if (!seen[i]) { | ||
dfs(A, i, seen); | ||
++ans; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
private: | ||
// Dfs on string A[i] | ||
void dfs(const vector<string>& A, int i, vector<bool>& seen) { | ||
seen[i] = true; | ||
for (int j = 0; j < A.size(); ++j) | ||
if (!seen[j] && isSimilar(A[i], A[j])) | ||
dfs(A, j, seen); | ||
} | ||
|
||
bool isSimilar(const string& X, const string& Y) { | ||
int diff = 0; | ||
for (int i = 0; i < X.length(); ++i) | ||
if (X[i] != Y[i] && ++diff > 2) | ||
return false; | ||
return true; | ||
} | ||
}; |