상위 150개 인터뷰쉬움

섬의 최대 면적

'섬의 최대 면적' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

m x n 이진 행렬 그리드가 제공됩니다. 섬은 4방향(수평 또는 수직)으로 연결된 1(땅을 나타냄)의 그룹입니다. 그리드의 네 모서리가 모두 물로 둘러싸여 있다고 가정할 수 있습니다.

섬의 면적은 섬에 있는 값이 1인 셀의 수입니다.

섬의 최대 면적을 그리드로 반환합니다. 섬이 없으면 0을 반환합니다.

maxAreaOfIsland(grid: List[List[int]]) -> int 함수를 작성하세요.

제약
  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 50
  • grid[i][j] is 0 or 1

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
Explanation

The maximum area of an island is 6, located in the lower-right part of the grid.

Example 2
Input
grid = [[0,0,0,0,0,0,0,0]]
Output
0
Explanation

There are no islands in the grid, so the max area is 0.

Need a Hint?
세트나 힙과 같은 그래프 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.