Number of Provinces | LeetCode

tanujav

Tanuja V

Posted on July 4, 2024

Number of Provinces | LeetCode
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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

💖 💪 🙅 🚩
tanujav
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