LeetCode Çözümleri – 200. Number of Islands [Amazon Mülakat Sorusu]

LeetCode içerisinde bulunan “Number of Islands” sorusunun açıklaması ve çözümü. Size verilen m * n boyutundaki bir grid üzerinde, kaç adet ada olduğunu bulmanız isteniyor. “Ada” tanımı olarak, 4 tarafı su ile çevrili olan kısımlar şeklinde belirtilmiş.

DFS algoritması nedir?

https://en.wikipedia.org/wiki/Depth-first_search

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

► LeetCode 200. Number of Islands: https://leetcode.com/problems/number-of-islands/

► Problem açıklaması:

Given an m x n 2d grid map of ‘1’s (land) and ‘0’s (water), return the number 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.

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