dexterpuru / leetcode_java

Archive for Leetcode solutions in Java
Creative Commons Zero v1.0 Universal
4 stars 15 forks source link

Number of islands #38

Closed Vaibhav22-Dev closed 4 years ago

Vaibhav22-Dev commented 4 years ago

Link:- https://leetcode.com/problems/number-of-islands/

… of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

A graph where all vertices are connected with each other has exactly one connected component, consisting of the whole graph. Such a graph with only one connected component is called a Strongly Connected Graph.

The problem can be easily solved by applying DFS() on each component. In each DFS() call, a component or a sub-graph is visited. We will call DFS on the next un-visited component. The number of calls to DFS() gives the number of connected components. BFS can also be used.

What is an island? A group of connected 1s forms an island. For example, the below matrix contains 4 islandsInput

i am marking similar group as X ,Y,Z,A,B {{1X, 1X, 0, 0, 0}, {0, 1X, 0, 0, 1Y}, {1X, 0, 0, 1Y, 1Y}, {0, 0, 0, 0, 0}, {1Z, 0, 1A, 0, 1B} } Output : 5 here if we talk about output that why output is 5 , then firstly male pair of 1's in matrix as you see in above example X is first group , Y pair is second group, hence Z has no pair so it is singly a group and similarly A and B So total 5 output comes

YourAKShaw commented 4 years ago

@Vaibhav22-Dev Kindly follow the contribution guidelines. Take a look at PR #17 for reference. You need to submit your solution in this format.

Vaibhav22-Dev commented 4 years ago

Hay, actually I don't know how to add readme.md file to write information according to community guidelines ,can u please help me.

Vaibhav22-Dev commented 4 years ago

can i add leetcode question link and question and pseudo code above my code

YourAKShaw commented 4 years ago

@Vaibhav22-Dev kindly checkout the other folders in our master branch and the README.md of the repo for steps. You need to keep the following in mind:

Hope it helps. Feel free to ask any questions.

dexterpuru commented 4 years ago

@Ak-Shaw i think we can close this PR now, its been 10 days now.

YourAKShaw commented 4 years ago

@dexterpuru sure.