#185 Counting the Number of Islands

Companies:
Wildberries X5
Themes:
AlgorithmsArraysPractice

There is an n by m matrix. 1 represents land, and 0 represents water. Count the number of islands. An island is an arbitrary piece of land that is not connected to other land areas. Diagonal connections do not count. Everything outside the matrix is water.

Text
Example 1:

Input: grid = [
    ["1", "1", "1", "1", "0"],
    ["1", "1", "0", "1", "0"],
    ["1", "1", "0", "0", "0"],
    ["0", "0", "0", "0", "0"]
]

Output: 1

Example 2:

Input: grid = [
    ["1", "1", "0", "0", "0"],
    ["1", "1", "0", "0", "0"],
    ["0", "0", "1", "0", "0"],
    ["0", "0", "0", "1", "1"]
]

Output: 3

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in