Number of Provinces | LeetCode
Tanuja V
Posted on July 4, 2024
class Solution {
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
int visited[] = new int[n];
int count = 0;
for(int i=0; i<n; i++){
if(visited[i]==0){
dfs(isConnected, i, visited, n);
count++;
}
}
return count;
}
void dfs(int grid[][], int u, int visited[], int n){
visited[u] = 1;
for(int i=0; i<n; i++){
if(grid[u][i]==1 && visited[i]==0)
dfs(grid, i, visited, n);
}
}
}
Thanks for reading :)
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀
If you enjoy my content, support me by following me on my other socials:
https://linktr.ee/tanujav7
💖 💪 🙅 🚩
Tanuja V
Posted on July 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript Day 5 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#56.Merge Intervals(Medium/JavaScript)
February 15, 2022