Interview Bootcamp
Vote
0% completed
Introduction to Island Pattern
You are given a grid of land and water. Count how many separate islands it holds, where an island is a group of land cells joined up, down, left or right.
1 1 0 0
1 0 0 1 there are 3 islands
0 0 1 0
The grid is a graph in disguise. Every cell is a node, and every cell is joined to the neighbour above it, below it, to its left and to its right. Nobody hands you an adjacency list, because you can work out the neighbours from the coordinates.
That means the whole problem is the graph traversal you already know, run in a loop:
- Walk every cell in the grid. 2
.....
.....
.....
Like the course? Get enrolled and start learning!
Z
zaid
· 3 years ago
title
Show 1 reply
A
Arul Prakash
· 4 years ago
hi