Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

144. Max Area of Island

MediumDay 43 of 60Graphs

You are given an m x n binary grid where 1 is land and 0 is water. An island is a group of 1s connected horizontally or vertically, and its area is the number of cells in it. Return the largest island area, or 0 if there is no land.

Example 1
Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
The island on the right side made of cells in rows 3 to 5 has 6 cells. Diagonal cells do not join islands.
Example 2
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0
Constraints
  • 1 <= m, n <= 50
  • grid[i][j] is 0 or 1
Hints
Frequently asked at: Amazon, Google, Meta, Bloomberg
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min