Skip to content

Commit 3f88899

Browse files
committed
graphs problems
1 parent 873c325 commit 3f88899

File tree

7 files changed

+279
-0
lines changed

7 files changed

+279
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
4+
5+
6+
int n=nums.size();
7+
multiset<long> s;
8+
if(n<=0 || k<=0)
9+
return false;
10+
11+
int i;
12+
for(i=0;i<n;i++)
13+
{
14+
long num=(long)nums[i];
15+
if(i>k)
16+
{
17+
s.erase(nums[i-k-1]);
18+
}
19+
20+
auto pos=s.lower_bound(num-t);
21+
22+
if(pos!=s.end() && (long)*pos-num<=t)
23+
return true;
24+
25+
s.insert(nums[i]);
26+
27+
}
28+
29+
return false;
30+
31+
32+
}
33+
};
34+
35+
//complexity: nlogk logk complexity for set operations, n times for each element in array
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
4+
5+
6+
unordered_map<int,int> mp;
7+
8+
int n=nums.size();
9+
10+
if(n<=0 || k<=0)
11+
return false;
12+
13+
14+
for(int i=0;i<n;i++)
15+
{
16+
if(i>k)
17+
{
18+
mp[nums[i-k-1]]--;
19+
}
20+
21+
mp[nums[i]]++;
22+
23+
if(mp[nums[i]]>1)
24+
return true;
25+
}
26+
27+
return false;
28+
}
29+
};

Leetcode/Graphs/flood-fill.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
4+
5+
int m=image.size(), n=image[0].size();
6+
7+
queue<pair<int,int>> q;
8+
q.push({sr,sc});
9+
int color=image[sr][sc];
10+
image[sr][sc]=newColor;
11+
vector<vector<int>> dir={{0,1}, {1,0}, {0,-1}, {-1,0}};
12+
vector<vector<bool>> visited(m, vector<bool>(n, false));
13+
14+
visited[sr][sc]=true;
15+
16+
while(!q.empty())
17+
{
18+
pair<int,int> temp=q.front();
19+
q.pop();
20+
21+
for(auto d: dir)
22+
{
23+
int x=temp.first+d[0];
24+
int y=temp.second+d[1];
25+
26+
if(x>=0 && x<m && y>=0 && y<n && image[x][y]==color && visited[x][y]==false)
27+
{
28+
image[x][y]=newColor;
29+
q.push({x,y});
30+
visited[x][y]=true;
31+
}
32+
}
33+
}
34+
35+
return image;
36+
}
37+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int maxAreaOfIsland(vector<vector<int>>& grid) {
4+
5+
int ans=INT_MIN;
6+
7+
int m=grid.size(), n=grid[0].size();
8+
9+
for(int i=0;i<m;i++)
10+
{
11+
for(int j=0;j<n;j++)
12+
{
13+
if(grid[i][j]==1)
14+
ans=max(ans, dfs(grid, i,j));
15+
}
16+
}
17+
if(ans==INT_MIN)
18+
return 0;
19+
else
20+
return ans;
21+
22+
23+
}
24+
25+
int dfs(vector<vector<int>> &grid, int x, int y)
26+
{
27+
if(x<0 || y<0 || x>=grid.size() || y>=grid[0].size() || grid[x][y]==0 || grid[x][y]==-1)
28+
return 0;
29+
30+
grid[x][y]=-1;
31+
32+
return 1+ dfs(grid,x+1,y)+dfs(grid, x, y+1)+ dfs(grid, x-1, y) + dfs(grid, x,y-1);
33+
34+
35+
36+
}
37+
};

Leetcode/Graphs/number-of-islands.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int numIslands(vector<vector<char>>& grid) {
4+
5+
int res=0;
6+
7+
for(int i=0;i<grid.size();i++)
8+
{
9+
for(int j=0;j<grid[0].size();j++)
10+
{
11+
if(grid[i][j]=='1')
12+
{
13+
dfs(grid,i,j);
14+
res++;
15+
}
16+
}
17+
}
18+
19+
return res;
20+
21+
}
22+
23+
void dfs(vector<vector<char>> &grid, int x, int y)
24+
{
25+
if(x<0 || x>=grid.size()|| y<0 || y>=grid[0].size() || grid[x][y]=='0')
26+
return;
27+
28+
grid[x][y]='0';
29+
30+
dfs(grid, x+1,y);
31+
dfs(grid, x,y+1);
32+
dfs(grid,x,y-1);
33+
dfs(grid, x-1,y);
34+
}
35+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int islandPerimeter(vector<vector<int>>& grid) {
4+
5+
int m=grid.size();
6+
int n=grid[0].size();
7+
8+
int res=0;
9+
10+
for(int i=0;i<m;i++)
11+
{
12+
for(int j=0;j<n;j++)
13+
{
14+
if(grid[i][j]==1)
15+
{
16+
if(i==0 || grid[i-1][j]==0)
17+
res++;
18+
if(i==m-1 || grid[i+1][j]==0)
19+
res++;
20+
if(j==0 || grid[i][j-1]==0)
21+
res++;
22+
23+
if(j==n-1 || grid[i][j+1]==0)
24+
res++;
25+
}
26+
}
27+
}
28+
29+
return res;
30+
31+
}
32+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Solution {
2+
public:
3+
int res;
4+
bool valid=false;
5+
string largestTimeFromDigits(vector<int>& arr) {
6+
7+
unordered_map<int,bool> used;
8+
vector<int> temp;
9+
res=0;
10+
backtrack(arr, temp, used);
11+
12+
string ans="";
13+
14+
if(!valid)
15+
return "";
16+
17+
int hr=res/100;
18+
int min=res%100;
19+
20+
if(hr==0)
21+
ans+="00";
22+
else if(hr/10==0)
23+
ans+="0"+to_string(hr);
24+
else
25+
ans+=to_string(hr);
26+
27+
ans+=":";
28+
29+
if(min==0)
30+
ans+="00";
31+
else if(min/10==0)
32+
ans+="0"+to_string(min);
33+
else
34+
ans+=to_string(min);
35+
36+
return ans;
37+
38+
39+
40+
41+
}
42+
43+
void backtrack(vector<int> arr, vector<int> temp, unordered_map<int,bool> used)
44+
{
45+
if(temp.size()==arr.size())
46+
{
47+
int hr=temp[0]*10+temp[1];
48+
int min=temp[2]*10+temp[3];
49+
50+
if(hr<24 && min < 60)
51+
{
52+
res=max(res,hr*100+min);
53+
valid=true;
54+
return;
55+
}
56+
}
57+
58+
for(int i=0;i<arr.size();i++)
59+
{
60+
if(used[i] || (i>0 && arr[i]==arr[i-1] && !used[i-1]))
61+
continue;
62+
63+
temp.push_back(arr[i]);
64+
used[i]=true;
65+
backtrack(arr, temp, used);
66+
used[i]=false;
67+
temp.pop_back();
68+
}
69+
70+
return;
71+
72+
}
73+
74+
};

0 commit comments

Comments
 (0)