interviewmind

Practice room

Call time 00:00Get the app
D
Dana · Interviewer
You
You
Captions

Press Play and Dana asks the question out loud.

Questions9/17

CodingMedium

Number of Islands

You’re given an m by n grid where each cell is 1 for land or 0 for water. Return the number of islands.

An island is a group of land cells connected horizontally or vertically, never diagonally. Everything beyond the grid’s edges is water.

Example 1

Inputgrid = [ [1,1,0,0,1], [1,0,0,1,1], [0,0,0,0,0], [0,1,1,0,1] ]Output4

Two islands along the top, a pair of cells in the bottom row, and a single cell in the bottom-right corner.

Example 2

Inputgrid = [[1,0],[0,1]]Output2

Cells that touch only at a corner aren’t connected.

Constraints

  • 1 ≤ m, n ≤ 300
  • grid[i][j] is 0 or 1

Tab indents. Press Escape, then Tab, to move focus out of the editor.

TypeScriptLn 1, Col 1
Practice room — InterviewMind