Skip to content

Commit

Permalink
Added Number Of Provinces Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoti-bhasin committed Oct 30, 2022
1 parent 9b0a65e commit ab4b7bd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions leetcode/Number_of_provinces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public:
int v;
void dfs(int node, vector<int> &vis,vector < int > list1[])
{
vis[node]=1;
for(auto it: list1[node])
{
if(!vis[it])
dfs(it, vis, list1);
}
}


int findCircleNum(vector<vector<int>>& isConnected) {
int v =isConnected.size();
vector<int>vis(v,0);
vector<int> list1[v];

for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
{
if(isConnected[i][j]==1 && i!=j)
{
list1[i].push_back(j);
list1[j].push_back(i);
}
}
}

int c=0;
for(int i=0;i<v;i++)
{
if(!vis[i])
{
c++;
dfs(i, vis, list1);
}
}
return c;

}
};

0 comments on commit ab4b7bd

Please sign in to comment.